当前位置:   article > 正文

MFC GB2312、UTF-8、unicode 之间转换_mfc unicode 下宽字节转gbk

mfc unicode 下宽字节转gbk
  1. //GB2312到UTF-8的转换
  2. static int GB2312ToUtf8(const char* gb2312, char* utf8)
  3. {
  4. int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
  5. wchar_t* wstr = new wchar_t[len+1];
  6. memset(wstr, 0, len+1);
  7. MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
  8. len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
  9. WideCharToMultiByte(CP_UTF8, 0, wstr, -1, utf8, len, NULL, NULL);
  10. if(wstr) delete[] wstr;
  11. return len;
  12. }
  13. //判断是否是utf8
  14. bool IsTextUTF8(const char* str, long length)
  15. {
  16. unsigned char chr;
  17. int nBytes = 0; // UFT8可用1-6个字节编码,ASCII用一个字节
  18. bool bAllAscii = true; // 如果全部都是ASCII, 说明不是UTF-8
  19. for (int i=0; i < length; i++)
  20. {
  21. chr = *(str + i);
  22. if ((chr&0x80) != 0) // 判断是否ASCII编码,如果不是,说明有可能是UTF-8, ASCII用7位编码,但用一个字节存,最高位标记为0,o0xxxxxxx
  23. {
  24. bAllAscii = false;
  25. }
  26. if (nBytes == 0) // 如果不是ASCII码,应该是多字节符,计算字节数
  27. {
  28. if (chr >= 0x80)
  29. {
  30. if (chr>=0xFC && chr<=0xFD)
  31. nBytes = 6;
  32. else if (chr>=0xF8)
  33. nBytes = 5;
  34. else if (chr>=0xF0)
  35. nBytes = 4;
  36. else if (chr>=0xE0)
  37. nBytes = 3;
  38. else if (chr>=0xC0)
  39. nBytes = 2;
  40. else
  41. return false;
  42. nBytes--;
  43. }
  44. }
  45. else // every char of ascii buffer looks like 10xxxxxx, except the first char
  46. {
  47. if( (chr&0xC0) != 0x80 )
  48. {
  49. return false;
  50. }
  51. nBytes--;
  52. }
  53. }
  54. if (nBytes > 0) // format error
  55. {
  56. return false;
  57. }
  58. if (bAllAscii) // if all chars are ascii, the buffer is not utf-8
  59. {
  60. return false;
  61. }
  62. return true;
  63. }
  64. //UTF-8到GB2312的转换
  65. static int Utf8ToGB2312(const char* utf8, char* gb2312)
  66. {
  67. int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
  68. wchar_t* wstr = new wchar_t[len+1];
  69. memset(wstr, 0, len+1);
  70. MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
  71. len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
  72. WideCharToMultiByte(CP_ACP, 0, wstr, -1, gb2312, len, NULL, NULL);
  73. if(wstr) delete[] wstr;
  74. return len;
  75. }
  76. //GB2312到Unicode的转换
  77. static int GB2312ToUnicode(const char* gb2312, char* unicode)
  78. {
  79. UINT nCodePage = 936; //GB2312
  80. int len = MultiByteToWideChar(nCodePage, 0, gb2312, -1, NULL, 0);
  81. wchar_t* wstr = new wchar_t[len+1];
  82. memset(wstr, 0, len+1);
  83. MultiByteToWideChar(nCodePage, 0, gb2312, -1, wstr, len);
  84. len = len*sizeof(wchar_t);
  85. memcpy(unicode, wstr, len);
  86. if(wstr) delete[] wstr;
  87. return len;
  88. }
  89. //Unicode到GB2312的转换
  90. static int UnicodeToGB2312(const char* unicode, int size, char*gb2312)
  91. {
  92. UINT nCodePage = 936; //GB2312
  93. wchar_t* wstr = new wchar_t[size/2+1];
  94. memcpy(wstr, unicode, size);
  95. int len = WideCharToMultiByte(nCodePage, 0, wstr, -1, NULL, 0, NULL, NULL);
  96. WideCharToMultiByte(nCodePage, 0, wstr, -1, gb2312, len, NULL, NULL);
  97. if(wstr) delete[] wstr;
  98. return len;
  99. }
  100. //UTF-8到Unicode的转换
  101. static int Utf8ToUnicode(const char* utf8, char*unicode)
  102. {
  103. int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
  104. wchar_t* wstr = new wchar_t[len+1];
  105. memset(wstr, 0, len+1);
  106. MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
  107. memcpy(unicode, wstr, len);
  108. if(wstr) delete[] wstr;
  109. return len;
  110. }
  111. //Unicode到UTF-8的转换
  112. static int UnicodeToUtf8(const char* unicode, int size, char* utf8)
  113. {
  114. wchar_t* wstr = new wchar_t[size/2+1];
  115. memcpy(wstr, unicode, size);
  116. int len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
  117. WideCharToMultiByte(CP_UTF8, 0, wstr, -1, utf8, len, NULL, NULL);
  118. if(wstr) delete[] wstr;
  119. return len;
  120. }

 

转自http://blog.csdn.net/seven407/article/details/7712823

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号