字符集互转的方法

来源:互联网 发布:明解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;}

原创粉丝点击