使用windows api實現任意兩種字元編碼的轉換

來源:互聯網
上載者:User

用到兩個函數:MultiByteToWideChar, WideCharToMultiByte。 使用這兩個API實現任意兩種字元編碼的轉換非常簡單:將源字串使用MultiByteToWideChar轉換成unicode編碼,再將轉換後的unicode編碼使用WideCharToMultiByte轉換成指定的字元編碼。 下面是一段示範程式:

  1. #include <windows.h>
  2. #include <tchar.h>
  3. #include <stdio.h>
  4. #include <locale.h>
  5. ////////////////////////////////////////////////////////////////////
  6. void hexdump(const void* addr, int bytes) {
  7.     int lines = bytes / 16, i = 0;
  8.     int j;
  9.     const unsigned char* pMem = (const unsigned char*)addr;
  10.     setlocale(LC_ALL, ".ACP");
  11.     while(i < lines) {
  12.         for(j = 0; j < 16; j++)
  13.             printf("%-4x", pMem[i * 16 + j]);
  14.         printf("/n");
  15.         for(j = 0; j < 16; j++) {
  16.             char ch = pMem[i*16+j];
  17.             printf("%-4c", isprint(ch) ? ch : '.');
  18.         }
  19.         printf("/n");
  20.         i++;
  21.     }
  22.     i = lines * 16;
  23.     for(j = i; j < bytes; j++)
  24.         printf("%-4x", pMem[j]);
  25.     printf("/n");
  26.     for(j = i; j < bytes; j++) {
  27.         char ch = pMem[j];
  28.         printf("%-4c", isprint(ch) ? ch : '.');
  29.     }
  30.     printf("/n");
  31. }
  32. ///////////////////////////////////////////////////////////////
  33. BOOL CALLBACK EnumCodePagesProc(
  34.   LPTSTR lpCodePageString   // code page identifier string
  35.   ) {
  36.     UINT uCodePage = atoi(lpCodePageString);
  37.     printf("uCodePage: %d/n", uCodePage);
  38.     CPINFOEX cpinfo;
  39.     GetCPInfoEx(uCodePage, 0, &cpinfo);
  40.     printf("MaxCharSize: %d/n"
  41.         "CodePage: %d/n"
  42.         "CodePageName: %s/n",
  43.         cpinfo.MaxCharSize,
  44.         cpinfo.CodePage,
  45.         cpinfo.CodePageName);
  46.     printf("DefaultChar:");
  47.     for(int i = 0;i < MAX_DEFAULTCHAR; i++)
  48.         printf("%3x", cpinfo.DefaultChar[i]);
  49.     printf("/nLeadByte:");
  50.     for(i = 0; i < MAX_LEADBYTES; i++)
  51.         printf("%3x", cpinfo.LeadByte[i]);
  52.     printf("/nUnicodeDefaultChar:");
  53.     wprintf(L"%x", cpinfo.UnicodeDefaultChar);
  54.     puts("/n");
  55.     return TRUE;
  56. }
  57. ////////////////////////////////////////////////////////////////
  58. int main() {
  59.     char szTest[] = "this²âÊÔtest";
  60.     printf("local settings: %s/n", setlocale(LC_CTYPE, ""));
  61.     puts("original string");
  62.     hexdump(szTest, sizeof(szTest));
  63.     puts("To Unicode");
  64.     wchar_t out[MAX_PATH] = {0};
  65.     MultiByteToWideChar(
  66.         CP_ACP, 
  67.         0,
  68.         szTest,
  69.         sizeof(szTest),
  70.         out,
  71.         MAX_PATH);
  72.     hexdump(out, wcslen(out) * sizeof(wchar_t));
  73.     puts("To UTF-8");
  74.     char utf8[MAX_PATH] = {0};
  75.     WideCharToMultiByte(
  76.         //54936, // GB18030--54936 CP_UTF8,
  77.         //950, // BIG5
  78.         65001,//CP_UTF8, // 65001, UTF-7:65000
  79.         0,
  80.         out,
  81.         wcslen(out),
  82.         utf8,
  83.         MAX_PATH,
  84.         NULL,
  85.         NULL
  86.         );
  87.     hexdump(utf8, lstrlen(utf8));
  88.     
  89. //  EnumSystemCodePages(EnumCodePagesProc, CP_INSTALLED);
  90.     return 0;
  91. }

輸出結果如下:
local settings: Chinese_People's Republic of China.936
original string
74  68  69  73  b2  e2  ca  d4  74  65  73  74  0
t   h   i   s   .   .   .   .   t   e   s   t   .
To Unicode
74  0   68  0   69  0   73  0   4b  6d  d5  8b  74  0   65  0
t   .   h   .   i   .   s   .   K   m   .   .   t   .   e   .
73  0   74  0
s   .   t   .
To UTF-8
74  68  69  73  e6  b5  8b  e8  af  95  74  65  73  74
t   h   i   s   .   .   .   .   .   .   t   e   s   t
可以看到“測試”兩個漢字在GBK編碼(字碼頁為936)中是十六進位的e2 b2 ca d4,在UNICODE中,則是十六進位的:4b 6d d5 8b,在UTF8中,則是十六進位的:e6  b5  8b  e8  af  95。
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.