ASCII,UTF-8,Unicode字符串相互转换(转)

来源:互联网 发布:华南理工大学软件学院 编辑:程序博客网 时间:2024/06/05 02:32
[cpp] view plaincopy
  1. #include  
  2. #include  
  3. #include  
  4. using namespace std;  
  5.   
  6. //utf8 转 Unicode  
  7.   
  8.   
  9. std::wstring Utf82Unicode(const std::string& utf8string)  
  10.  
  11.     int widesize ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, NULL, 0);  
  12.     if (widesize == ERROR_NO_UNICODE_TRANSLATION)  
  13.      
  14.         throw std::exception("Invalid UTF-8 sequence.");  
  15.      
  16.     if (widesize == 0)  
  17.      
  18.         throw std::exception("Error in conversion.");  
  19.      
  20.    
  21.     std::vector<<span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); background-color: inherit; font-weight: bold;">wchar_tresultstring(widesize);  
  22.    
  23.     int convresult ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0], widesize);  
  24.    
  25.     if (convresult != widesize)  
  26.      
  27.         throw std::exception("La falla!");  
  28.      
  29.    
  30.     return std::wstring(&resultstring[0]);  
  31.  
  32.   
  33.   
  34. //unicode 转为 ascii  
  35.   
  36.   
  37. std::string WideByte2Acsi(std::wstring& wstrcode)  
  38.  
  39.     int asciisize ::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, NULL, 0, NULL, NULL);  
  40.     if (asciisize == ERROR_NO_UNICODE_TRANSLATION)  
  41.      
  42.         throw std::exception("Invalid UTF-8 sequence.");  
  43.      
  44.     if (asciisize == 0)  
  45.      
  46.         throw std::exception("Error in conversion.");  
  47.      
  48.     std::vector<<span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); background-color: inherit; font-weight: bold;">charresultstring(asciisize);  
  49.     int convresult =::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, &resultstring[0], asciisize, NULL, NULL);  
  50.    
  51.     if (convresult != asciisize)  
  52.      
  53.         throw std::exception("La falla!");  
  54.      
  55.    
  56.     return std::string(&resultstring[0]);  
  57.  
  58.   
  59.   
  60.    
  61.   
  62.   
  63. //utf-8 转 ascii  
  64.   
  65.   
  66. std::string UTF_82ASCII(std::string& strUtf8Code)  
  67.  
  68.     std::string strRet("");  
  69.     //先把 utf8 转为 unicode  
  70.     std::wstring wstr Utf82Unicode(strUtf8Code);  
  71.     //最后把 unicode 转为 ascii  
  72.     strRet WideByte2Acsi(wstr);  
  73.     return strRet;  
  74.  
  75.   
  76.   
  77. ///////////////////////////////////////////////////////////////////////  
  78.   
  79.   
  80. //ascii 转 Unicode  
  81.   
  82.   
  83. std::wstring Acsi2WideByte(std::string& strascii)  
  84.  
  85.     int widesize MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0);  
  86.     if (widesize == ERROR_NO_UNICODE_TRANSLATION)  
  87.      
  88.         throw std::exception("Invalid UTF-8 sequence.");  
  89.      
  90.     if (widesize == 0)  
  91.      
  92.         throw std::exception("Error in conversion.");  
  93.      
  94.     std::vector<<span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); background-color: inherit; font-weight: bold;">wchar_tresultstring(widesize);  
  95.     int convresult MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, &resultstring[0], widesize);  
  96.   
  97.   
  98.     if (convresult != widesize)  
  99.      
  100.         throw std::exception("La falla!");  
  101.      
  102.    
  103.     return std::wstring(&resultstring[0]);  
  104.  
  105.   
  106.   
  107. //Unicode 转 Utf8  
  108.   
  109.   
  110. std::string Unicode2Utf8(const std::wstring& widestring)  
  111.  
  112.     int utf8size ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);  
  113.     if (utf8size == 0)  
  114.      
  115.         throw std::exception("Error in conversion.");  
  116.      
  117.    
  118.     std::vector<<span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); background-color: inherit; font-weight: bold;">charresultstring(utf8size);  
  119.    
  120.     int convresult ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL);  
  121.    
  122.     if (convresult != utf8size)  
  123.      
  124.         throw std::exception("La falla!");  
  125.      
  126.    
  127.     return std::string(&resultstring[0]);  
  128.  
  129.   
  130.   
  131. //ascii 转 Utf8  
  132.   
  133.   
  134. std::string ASCII2UTF_8(std::string& strAsciiCode)  
  135.  
  136.     std::string strRet("");  
  137.     //先把 ascii 转为 unicode  
  138.     std::wstring wstr Acsi2WideByte(strAsciiCode);  
  139.     //最后把 unicode 转为 utf8  
  140.     strRet Unicode2Utf8(wstr);  
  141.     return strRet;  
  142. }  
0 0