UTF8 MB Uncode 编码转换

  1. // 多字节编码转为UTF8编码
  2. bool MBToUTF8(vector<char>& pu8, const char* pmb, int32 mLen)
  3. {
  4. // convert an MBCS string to widechar
  5. int32 nLen = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, NULL, 0);
  6. WCHAR* lpszW = NULL;
  7. try
  8. {
  9. lpszW = new WCHAR[nLen];
  10. }
  11. catch(bad_alloc &memExp)
  12. {
  13. return false;
  14. }
  15. int32 nRtn = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, lpszW, nLen);
  16. if(nRtn != nLen)
  17. {
  18. delete[] lpszW;
  19. return false;
  20. }
  21. // convert an widechar string to utf8
  22. int32 utf8Len = WideCharToMultiByte(CP_UTF8, 0, lpszW, nLen, NULL, 0, NULL, NULL);
  23. if (utf8Len <= 0)
  24. {
  25. return false;
  26. }
  27. pu8.resize(utf8Len);
  28. nRtn = WideCharToMultiByte(CP_UTF8, 0, lpszW, nLen, &*pu8.begin(), utf8Len, NULL, NULL);
  29. delete[] lpszW;
  30. if (nRtn != utf8Len)
  31. {
  32. pu8.clear();
  33. return false;
  34. }
  35. return true;
  36. }
  37. // UTF8编码转为多字节编码
  38. bool UTF8ToMB(vector<char>& pmb, const char* pu8, int32 utf8Len)
  39. {
  40. // convert an UTF8 string to widechar
  41. int32 nLen = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, NULL, 0);
  42. WCHAR* lpszW = NULL;
  43. try
  44. {
  45. lpszW = new WCHAR[nLen];
  46. }
  47. catch(bad_alloc &memExp)
  48. {
  49. return false;
  50. }
  51. int32 nRtn = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, lpszW, nLen);
  52. if(nRtn != nLen)
  53. {
  54. delete[] lpszW;
  55. return false;
  56. }
  57. // convert an widechar string to Multibyte
  58. int32 MBLen = WideCharToMultiByte(CP_ACP, 0, lpszW, nLen, NULL, 0, NULL, NULL);
  59. if (MBLen <=0)
  60. {
  61. return false;
  62. }
  63. pmb.resize(MBLen);
  64. nRtn = WideCharToMultiByte(CP_ACP, 0, lpszW, nLen, &*pmb.begin(), MBLen, NULL, NULL);
  65. delete[] lpszW;
  66. if(nRtn != MBLen)
  67. {
  68. pmb.clear();
  69. return false;
  70. }
  71. return true;
  72. }
  73. // 多字节编码转为Unicode编码
  74. bool MBToUnicode(vector<wchar_t>& pun, const char* pmb, int32 mLen)
  75. {
  76. // convert an MBCS string to widechar
  77. int32 uLen = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, NULL, 0);
  78. if (uLen<=0)
  79. {
  80. return false;
  81. }
  82. pun.resize(uLen);
  83. int32 nRtn = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, &*pun.begin(), uLen);
  84. if (nRtn != uLen)
  85. {
  86. pun.clear();
  87. return false;
  88. }
  89. return true;
  90. }
  91. //Unicode编码转为多字节编码
  92. bool UnicodeToMB(vector<char>& pmb, const wchar_t* pun, int32 uLen)
  93. {
  94. // convert an widechar string to Multibyte
  95. int32 MBLen = WideCharToMultiByte(CP_ACP, 0, pun, uLen, NULL, 0, NULL, NULL);
  96. if (MBLen <=0)
  97. {
  98. return false;
  99. }
  100. pmb.resize(MBLen);
  101. int nRtn = WideCharToMultiByte(CP_ACP, 0, pun, uLen, &*pmb.begin(), MBLen, NULL, NULL);
  102. if(nRtn != MBLen)
  103. {
  104. pmb.clear();
  105. return false;
  106. }
  107. return true;
  108. }
  109. // UTF8编码转为Unicode
  110. bool UTF8ToUnicode(vector<wchar_t>& pun, const char* pu8, int32 utf8Len)
  111. {
  112. // convert an UTF8 string to widechar
  113. int32 nLen = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, NULL, 0);
  114. if (nLen <=0)
  115. {
  116. return false;
  117. }
  118. pun.resize(nLen);
  119. int32 nRtn = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, &*pun.begin(), nLen);
  120. if(nRtn != nLen)
  121. {
  122. pun.clear();
  123. return false;
  124. }
  125. return true;
  126. }
  127. // Unicode编码转为UTF8
  128. bool UnicodeToUTF8(vector<char>& pu8, const wchar_t* pun, int32 uLen)
  129. {
  130. // convert an widechar string to utf8
  131. int32 utf8Len = WideCharToMultiByte(CP_UTF8, 0, pun, uLen, NULL, 0, NULL, NULL);
  132. if (utf8Len<=0)
  133. {
  134. return false;
  135. }
  136. pu8.resize(utf8Len);
  137. int32 nRtn = WideCharToMultiByte(CP_UTF8, 0, pun, uLen, &*pu8.begin(), utf8Len, NULL, NULL);
  138. if (nRtn != utf8Len)
  139. {
  140. pu8.clear();
  141. return false;
  142. }
  143. return true;
  144. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值