字符集互转的方法
来源:互联网 发布:明解c语言入门pdf下载 编辑:程序博客网 时间:2024/06/14 12:39
#include <string>#include <boost/locale.hpp>#include <boost/program_options/detail/convert.hpp>#include <boost/program_options/detail/utf8_codecvt_facet.hpp>#include <codecvt>#include <string>// convert UTF-8 string to wstringstd::wstring utf8_to_wstring(const std::string& str){ std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv; return myconv.from_bytes(str);}// convert wstring to UTF-8 stringstd::string wstring_to_utf8(const std::wstring& str){ std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv; return myconv.to_bytes(str);}void f() { int x=0; x += 1;}intmain(void){ char utf8_str[] = { 0xE6 ,0xB5 ,0x8B ,0xE8 ,0xAF ,0x95, 0x00 }; //"测试"的utf8编码 char gbk_str[] = { 0xB2 ,0xE2 ,0xCA ,0xD4 ,0x00 }; //"测试"的GBK编码 wchar_t ucs16_str[] ={ 0x6D4B ,0x8BD5 ,0x0000 }; //"测试"的unicode 16 BE 编码 std::string ret1; std::wstring ret2; //GBK字符串 转 utf8字符串 ret1 = boost::locale::conv::to_utf<char>(gbk_str, "GBK"); //GBK字符串 转 unicode字符串 ret2 = boost::locale::conv::to_utf<wchar_t>(gbk_str, "GBK"); //utf8字符串 转 GBK(由string保存的) ret1 = boost::locale::conv::from_utf<char>(utf8_str, "GBK"); //utf8字符串 转 unicode(由wstring保存的) ret2 = boost::locale::conv::utf_to_utf<wchar_t>(utf8_str); //错误的代码 //ret2 = boost::locale::conv::utf_to_utf<wchar_t>(utf8_str,"GBK"); X //直接导致CPU 100%。因为它调用了utf_to_utf(const char* begin, const char* end) //utf8_str和"GBK"不是匹配的迭代器 //unicode字符串 转 utf8 ret1 = boost::locale::conv::utf_to_utf<char>(ucs16_str, ucs16_str+2); //unicode字符串 转 GBK字符串。注意,from...函数,模板参数同函数的文本参数 // to...函数,模板参数同返回值 ret1 = boost::locale::conv::from_utf<wchar_t>(ucs16_str, ucs16_str+2, "GBK"); //C++11 提供的utf8 转 ucs16 ret2 = utf8_to_wstring(utf8_str); //C++11 提供的ucs16 转 utf8 ret1 = wstring_to_utf8(ucs16_str); return 0;}
阅读全文
0 0
- 字符集互转的方法
- Linux字符集的修改方法
- Linux字符集的修改方法
- Linux字符集的修改方法
- Linux字符集的修改方法
- Linux字符集的修改方法
- 查看MySQL字符集的方法
- PHP自动判断字符集并转码的方法
- ubuntu 配置系统字符集的方法
- 修改oracle数据库字符集的方法
- 不同字符集倒库的方法
- 修改oracle数据库字符集的方法
- Syabse的服务器端默认字符集修改方法
- Windows Notepad 判断字符集的方法
- 修改mysql默认字符集的方法
- oracle client端设置字符集的方法
- Syabse的服务器端默认字符集修改方法
- 修改mysql默认字符集的方法
- Dubbo与spring-boot使用
- hdu2255 奔小康赚大钱【最大权完美匹配】
- iOS hidesBottomBarWhenPushed的正确用法
- java编程语言关键字
- 原生js事件委托与简单的jquery事件委托方法
- 字符集互转的方法
- 在wap中启动 app(利用URL Scheme打开APP并传递数据 )
- 游戏中的角色渲染技术之皮肤篇
- HTTP网络中的 Multiplexing
- Java中super的用法 ____简单粗暴
- HUD2020
- Java中Synchronized的用法
- 转行的艰难之 找实习
- JDK安装后如何配置环境?