C++編程:ASCII,UTF-8,Unicode字符串相互轉換


[cpp] view plain copy
  1. #include<string>  
  2. #include<windows.h>  
  3. #include<vector>  
  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<wchar_t> resultstring(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<char> resultstring(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<wchar_t> resultstring(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<char> resultstring(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. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章