Use windows api to convert any two types of character encoding

Source: Internet
Author: User

Two functions are used: multibytetowidechar and widechartomultibyte. Using these two APIs to convert any two types of character encoding is very simple: converting the source string to Unicode using multibytetowidechar, then, use widechartomultibyte to convert the converted unicode encoding to the specified character encoding. The following is a demo program:

  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. unicodedefachar char );
  54. Puts ("/n ");
  55. Return TRUE;
  56. }
  57. //////////////////////////////////////// ////////////////////////
  58. Int main (){
  59. Char szTest [] = "this ² â zookeeper 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. // EnumSystemCodePages (EnumCodePagesProc, CP_INSTALLED );
  89. Return 0;
  90. }

The output result is as follows:
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
We can see that the two Chinese characters "test" are in GBK encoding (the code page is 936) in hexadecimal format e2 b2 ca d4. In UNICODE, they are in hexadecimal format: 4b 6d d5 8b, in UTF8, is hexadecimal: e6 b5 8b e8 af 95.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.