<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

    C++轉換unicode utf-8 gb2312編碼

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接

    windows開發環境下用VC++6.0 對unicode 、utf-8、 gb2312 三種編碼格式之間的轉換方法:

     

    [cpp] view plaincopy  
    1. #include <iostream>  
    2. #include <string>  
    3. #include <Windows.h>  
    4. using namespace std;  
    5.   
    6. void unicodeToUTF8(const wstring &src, string& result)  
    7. {  
    8.     int n = WideCharToMultiByte( CP_UTF8, 0, src.c_str(), -1, 0, 0, 0, 0 );  
    9.     result.resize(n);  
    10.     ::WideCharToMultiByte( CP_UTF8, 0, src.c_str(), -1, (char*)result.c_str(), result.length(), 0, 0 );  
    11. }  
    12.   
    13. void unicodeToGB2312(const wstring& wstr , string& result)  
    14. {  
    15.     int n = WideCharToMultiByte( CP_ACP, 0, wstr.c_str(), -1, 0, 0, 0, 0 );  
    16.     result.resize(n);  
    17.     ::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, (char*)result.c_str(), n, 0, 0 );  
    18. }  
    19.   
    20. void utf8ToUnicode(const string& src, wstring& result)  
    21. {  
    22.     int n = MultiByteToWideChar( CP_UTF8, 0, src.c_str(), -1, NULL, 0 );  
    23.     result.resize(n);  
    24.     ::MultiByteToWideChar( CP_UTF8, 0, src.c_str(), -1, (LPWSTR)result.c_str(), result.length());  
    25. }  
    26.   
    27. void gb2312ToUnicode(const string& src, wstring& result)  
    28. {  
    29.     int n = MultiByteToWideChar( CP_ACP, 0, src.c_str(), -1, NULL, 0 );  
    30.     result.resize(n);  
    31.     ::MultiByteToWideChar( CP_ACP, 0, src.c_str(), -1, (LPWSTR)result.c_str(), result.length());  
    32. }  
    33.   
    34. void printByte(string str)  
    35. {  
    36.     int i=0;  
    37.     for (i=0; i<str.length(); i++)  
    38.     {  
    39.         printf("%X ",(unsigned char)str.at(i));  
    40.     }  
    41.     printf("\n");  
    42. }  
    43.   
    44. void wprintByte(wstring str)  
    45. {  
    46.     int i=0;  
    47.     for (i=0; i<str.length()*sizeof(wchar_t); i++)  
    48.     {  
    49.         printf("%X ",*((unsigned char*)str.c_str()+i));  
    50.     }  
    51.     printf("\n");  
    52. }  
    53.   
    54. int main()  
    55. {  
    56.     string strText = "AB漢字";  
    57.     string strUTF8;  
    58.     wstring wstrUnicode;  
    59.     string strGB2312;  
    60.   
    61.     gb2312ToUnicode(strText, wstrUnicode);  
    62.     printf("Unicode=");  
    63.     wprintByte(wstrUnicode);  
    64.   
    65.     unicodeToUTF8(wstrUnicode, strUTF8);  
    66.     printf("UTF-8  =");  
    67.     printByte(strUTF8);  
    68.       
    69.     utf8ToUnicode(strUTF8,wstrUnicode);  
    70.     printf("Unicode=");  
    71.     wprintByte(wstrUnicode);  
    72.       
    73.     unicodeToGB2312(wstrUnicode,strGB2312);  
    74.     printf("GB2312 =");  
    75.     printByte(strGB2312);  
    76.   
    77.     return 0;        
    78. }  

    這里用“AB漢字”這樣一個字符串做測試,它的ASCII編碼為41 42 BA BA D7 D6
    輸出結果:

     

    RFID設備管理軟件

    RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全