在编程的时候各种各样的编码转换让人挠头。有的地方需要宽字符有的地方需要窄字符,需要转换。有的地方需要UTF-8编码,有的地方需要UTF-16编码,需要转换。各种各样的编码转换让人头疼,这里呢,就梳理一下各种各种的编码转换方法。让你不再为这个问题头疼。需要注意的是有些方法引用了windows的API无法实现跨平台。

获取文件的编码类型

//获取文件的编码
//找不到文件头的信息的时候可能是ASCII,也可能是UTF8不带bom头的编码需要自己指定一下
enum CODETYPE { ASCII, UTF8, UTF8BOM, UTF16LE, UTF16BE, UTF32LE, UTF32BE, UNKNOWN_TYPE };
CODETYPE GetFileCodeType(const char* file_path)
{FILE* file = fopen(file_path, "rb");if (!file){return UNKNOWN_TYPE;}int cur_pos = ftell(file);fseek(file, 0L, SEEK_END);int file_size = ftell(file);fseek(file, cur_pos, SEEK_SET);char* buffer = (char*)malloc(file_size);fread(buffer, 1, file_size, file);fclose(file);if (file_size >= 4){if (memcmp(buffer, "\xFF\xFE\x00\x00", 4) == 0)return UTF32LE;if (memcmp(buffer, "\x00\x00\xfe\xff", 4) == 0)return UTF32BE;}if (file_size >= 3){if (memcmp(buffer, "\xEF\xBB\xBF", 3) == 0) return UTF8BOM;}if (file_size >= 2){if (memcmp(buffer, "\xFF\xFE", 2) == 0) return UTF16LE;if (memcmp(buffer, "\xFE\xFF", 2) == 0) return UTF16BE;}return UTF8;
}

wchar_t转char

char* wchar_to_char(const wchar_t* wchar)
{char * m_char;int len = WideCharToMultiByte(CP_ACP, 0, wchar, (int)wcslen(wchar), NULL, 0, NULL, NULL);m_char = new char[len + 1];WideCharToMultiByte(CP_ACP, 0, wchar, (int)wcslen(wchar), m_char, len, NULL, NULL);m_char[len] = '\0';return m_char;
}
//结果是分配的内存记得free掉

char转wchar_t

wchar_t* char_to_wchar(char *s){
int w_nlen=MultiByteToWideChar(CP_ACP,0,s,-1,NULL,0);
wchar_t *ret;
ret=(wchar_t*) malloc(sizeof(WCHAR)*w_nlen);
memset(ret,0,sizeof(ret));
MultiByteToWideChar(CP_ACP,0,s,-1,ret,w_nlen);
return ret;
}
//结果是分配的内存记得free掉

wstring转string

std::string wstr_to_str(const std::wstring &wc)
{int lenMB = ::WideCharToMultiByte(CP_ACP, 0, wc.c_str(), static_cast<int>(wc.length()), NULL, 0, NULL, NULL);std::unique_ptr<char> mb(new char[lenMB]());::WideCharToMultiByte(CP_ACP, 0, wc.c_str(), static_cast<int>(wc.length()), mb.get(), lenMB, NULL, NULL);return std::string(mb.get(), lenMB);
}

string转wstring

std::wstring str_to_wstr(const std::string &mb)
{int lenWC = ::MultiByteToWideChar(CP_ACP, 0, mb.c_str(), static_cast<int>(mb.length()), NULL, 0);std::unique_ptr<wchar_t> wc(new wchar_t[lenWC]());::MultiByteToWideChar(CP_ACP, 0, mb.c_str(), static_cast<int>(mb.length()), wc.get(), lenWC);return std::wstring(wc.get(), lenWC);
}

utf-8 wstring转utf-8 string

std::string wstr_to_utf8str(const std::wstring& wc)
{int lenUTF8 = WideCharToMultiByte(CP_UTF8, 0, wc.c_str(), static_cast<int>(wc.length()), NULL, 0, NULL, NULL);std::unique_ptr<char> utf8(new char[lenUTF8]());WideCharToMultiByte(CP_UTF8, 0, wc.c_str(), static_cast<int>(wc.length()), utf8.get(), lenUTF8, NULL, NULL);return std::string(utf8.get(), lenUTF8);
}

utf-8 string转utf-8 wstring

std::wstring utf8str_to_wstr(const std::string &utf8)
{int lenWC = ::MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), static_cast<int>(utf8.length()), NULL, 0);std::unique_ptr<wchar_t> wc(new wchar_t[lenWC]());::MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), static_cast<int>(utf8.length()), wc.get(), lenWC);return std::wstring(wc.get(), lenWC);
}

UTF-8编码转多字节编码

std::string utf8_to_multibytes(const std::string& utf8)
{int lenWC = ::MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), static_cast<int>(utf8.length()), NULL, 0);std::unique_ptr<wchar_t> wc(new wchar_t[lenWC]());::MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), static_cast<int>(utf8.length()), wc.get(), lenWC);int lenMB = ::WideCharToMultiByte(CP_ACP, 0, wc.get(), lenWC, NULL, 0, NULL, NULL);std::unique_ptr<char> mb(new char[lenMB]());::WideCharToMultiByte(CP_ACP, 0, wc.get(), lenWC, mb.get(), lenMB, NULL, NULL);return std::string(mb.get(), lenMB);
}

多字节编码转UTF-8编码

std::string multibytes_to_utf8(const std::string &mb)
{int lenWC = ::MultiByteToWideChar(CP_ACP, 0, mb.c_str(), static_cast<int>(mb.length()), NULL, 0);std::unique_ptr<wchar_t>  wc(new wchar_t[lenWC]());::MultiByteToWideChar(CP_ACP, 0, mb.c_str(), static_cast<int>(mb.length()), wc.get(), lenWC);int lenUTF8 = ::WideCharToMultiByte(CP_UTF8, 0, wc.get(), lenWC, NULL, 0, NULL, NULL);std::unique_ptr<char> utf8(new char[lenUTF8]());::WideCharToMultiByte(CP_UTF8, 0, wc.get(), lenWC, utf8.get(), lenUTF8, NULL, NULL);return std::string(utf8.get(), lenUTF8);
}

大端对齐转小端对齐

void big_to_little(wchar_t* src, unsigned int size)
{for (usigned int iix = 0; iix < size; ++iix, ++src) {*src = (((*src) & 0xff00) >> 8) | (((*src) & 0x00ff) << 8);}
}

编码转换参考范例大全相关推荐

  1. python codecs模块(用于执行编码转换之类的)

    用于执行编码转换 参考文章:尝试修改LabelImg,将以对顶角画框改成以对角线相交点画框

  2. python编码大全_Python3中的编码转换大全(不定期更新)

    Python3编码转换大全 进制转换 其他进制转十进制 2 -> 10 int('1100',2) 12 8 -> 10 int('1100',8) 576 16 -> 10 int ...

  3. java中文字符乱码编码转换大全

    java中文字符乱码编码转换大全 2014-09-26 13:59 595人阅读 评论(0) 收藏 举报 本文章已收录于: 版权声明:本文为博主原创文章,未经博主允许不得转载. System.out. ...

  4. Google Android SDK开发范例大全

    1. 图书信息: Google Android SDK开发范例大全(第2版)     人民邮电出版社 2010-6-1 0:00:00 余志龙;陈昱勋;郑名杰;陈小凤;郭秩均 79 元 ISBN:97 ...

  5. Google Android SDK开发范例大全(第2版)

    内容简介 <Google Android SDK开发范例大全(第2版)>在上一版的基础上,以Android手机应用程序开发(采用AndroidSDK2.1)为主题,通过160多个范例全面且 ...

  6. Google Android SDK开发范例大全(第3版)

    查看书籍详细信息: Google Android SDK开发范例大全(第3版)当当网 编辑推荐 Android开发经典畅销书!前两版均为同类最畅销,新版最引人注目的特点是:架构清楚易使用:所有范例程序 ...

  7. Google App Engine for Java下的URL编码转换问题

    URL编码问题 此部分参考英文资料: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm http://www.w3school ...

  8. 关于Laravel中使用response()方法调用json()返回数据unicode编码转换的问题解决

    关于Laravel中使用response()方法调用json()返回数据unicode编码转换的问题解决 参考文章: (1)关于Laravel中使用response()方法调用json()返回数据un ...

  9. java linux urlencode_java字符编码转换研究(转)

    1. 概述 本文主要包括以下几个方面:编码基本知识,java,系统软件,url,工具软件等. 在下面的描述中,将以"中文"两个字为例,经查表可以知道其GB2312编码是" ...

  10. python utf-8编码转换中文_python实现unicode转中文及转换默认编码的方法

    本文实例讲述了python实现unicode转中文及转换默认编码的方法.分享给大家供大家参考,具体如下: 一.在爬虫抓取网页信息时常需要将类似"\u4eba\u751f\u82e6\u77e ...

最新文章

  1. The import java.util cannot be resolved The import javax.servlet cannot be resolved
  2. Android 添加菜单项
  3. 【ijkplayer】编译 Android 版本的 ijkplayer ② ( 切换到 k0.8.8 分支 | 执行 init-android.sh 脚本进行初始化操作 )
  4. redis持久化策略梳理及主从环境下的策略调整记录
  5. 嵌入式系统UBOOT
  6. 54 SD配置-定价配置-分配条件类型到条件排斥组
  7. JS点击获取验证码后60秒内禁止重新获取(防刷新)
  8. java创建线程的两种方法是_java创建线程的两种方法
  9. socket和URLConnection
  10. Java匹马行天下之学编程的起点——走进编程的殿堂
  11. 「leetcode」39. 组合总和【回溯算法】详解!
  12. 做手机系统,鸿蒙怎样才有机会
  13. rhino编程语言c井,Rhino插件开发:RhinoScript脚本教程(4):VBScript基础
  14. 10个5G应用优秀案例!工业互联网、智慧城市、智慧医疗等都在这里
  15. WebApp实时开源框架Clouda入门使用与记录
  16. 西安交通大学电子图书站点被黑
  17. A Survey on Deep Transfer Learning 2018 翻译
  18. python怎么处理中英文符号网名_英文带符号的网名_英文网名带符号加中文
  19. 四万字32图,Kafka知识体系保姆级教程宝典
  20. 中级微观经济学:Chap 31 行为经济学

热门文章

  1. 计算机博士复试英语自我介绍,博士复试自我介绍中英文
  2. 猜数字游戏 由计算机,猜数字游戏实验报告
  3. vue获取内外网ip地址
  4. 大学计算机基础教程第12章软件技术基础
  5. pandas 错误 ValueError: ‘Lengths must match to compare‘
  6. 微软人工智能公开课.md
  7. mysql 内联 外联_sql中的内联和外联(简单用法)
  8. 【CityHunter】Unity3D设计AR探索模式
  9. Server:基本的服务器
  10. css中找不到bordercolor,CSS里bordercolor要怎样使用