from http://blog.csdn.net/frankiewang008/article/details/12832239

  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. }

多字节与UTF-8、Unicode之间的转换的更多相关文章

  1. MultiByteToWideChar和WideCharToMultiByte用法详解, ANSI和UNICODE之间的转换

    //========================================================================//TITLE://    MultiByteToW ...

  2. C# - 汉字与unicode之间的转换

    /// <summary> /// 字符串转Unicode码 /// </summary> /// <returns>The to unicode.</ret ...

  3. java中unicode utf-8以及汉字之间的转换工具类

    1.       汉字字符串与unicode之间的转换 1.1          stringToUnicode /** * 获取字符串的unicode编码 * 汉字"木"的Uni ...

  4. 举例说明Unicode 和UTF-8之间的转换

    1)写这篇博客的原因 首先我要感谢这篇博客,卡了很久,看完下面这篇博客终于明白Unicode怎么转换成UTF-8了. https://blog.csdn.net/qq_32252957/article ...

  5. Unicode和UTF-8之间的转换

    转自:http://www.cnblogs.com/xdotnet/archive/2007/11/23/unicode_and_utf8.html#undefined 最近在用VC++开发一个小工具 ...

  6. 汉字编码(【Unicode】 【UTF-8】 【Unicode与UTF-8之间的转换】 【汉字 Unicode 编码范围】【中文标点Unicode码】【GBK编码】【批量获取汉字UNICODE码】)

    Unicode与UTF-8互转(C语言实现):http://blog.csdn.net/tge7618291/article/details/7599902 汉字 Unicode 编码范围:http: ...

  7. python中unicode, hex, bin之间的转换

    python中unicode, hex, bin之间的转换 背景 在smb中有个feature change notify, 需要改动文件权限dacl,然后确认是否有收到notify.一直得不到这个d ...

  8. [Python] 中文编码问题:raw_input输入、文件读取、变量比较等str、unicode、utf-8转换问题

    最近研究搜索引擎.知识图谱和Python爬虫比较多,中文乱码问题再次浮现于眼前.虽然市面上讲述中文编码问题的文章数不胜数,同时以前我也讲述过PHP处理数据库服务器中文乱码问题,但是此处还是准备简单做下 ...

  9. CString-int-string-char-BSTR之间的转换

    一.CString, int, string, char*之间的转换 string 转 CString CString.Format("%s", string.c_str());c ...

随机推荐

  1. CA证书申请、认证原理

    (一) 证书的申请 密钥文件的格式用OpenSSL生成的就只有PEM和DER两种格式,PEM的是将密钥用base64编码表示出来的,直接打开你能看到一串的英文字母,DER格式是二进制的密钥文件,直接打 ...

  2. Java之OutOfMemoryError简单分析

    Java之OutOfMemoryError简单分析 最近编码遇到了Java内存溢出的问题,所以就想顺便总结一下几种导致Java内存溢出的栗子,以及碰到Java内存溢出要如何去解决. Java堆溢出 J ...

  3. 【Palindrome Partitioning】cpp

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  4. leetcode 【 Search in Rotated Sorted Array II 】python 实现

    题目: 与上一道题几乎相同:不同之处在于array中允许有重复元素:但题目要求也简单了,只要返回true or false http://www.cnblogs.com/xbf9xbf/p/42545 ...

  5. android 内存说明

    MemoryInfo的Field如下 dalvikPrivateDirty: The private dirty pages used by dalvik. dalvikPss :The propor ...

  6. python-day5-格式化输入

    python格式化输入包含'%'调用,及format方法 使用‘%’进行格式化输出 #最简单的字符串传参 tpl='i am %s '%'alex' >>>i am alex #字符 ...

  7. backpropagation算法示例

    backpropagation算法示例 下面举个例子,假设在某个mini-batch的有样本X和标签Y,其中\(X\in R^{m\times 2}, Y\in R^{m\times 1}\),现在有 ...

  8. html之表单标签

    表单标签的属性: 用于向服务器传输数据 表单能够包含input元素,比如文本字段,复选框,单选框,提交按钮等等 表单还可以包含textarea(简介之类的),select(下拉),fieldset和l ...

  9. 微信公众平台OAuth2.0网页授权

    微信公众平台最近新推出微信认证,认证后可以获得高级接口权限,其中一个是OAuth2.0网页授权,很多朋友在使用这个的时候失败了或者无法理解其内容,希望我出个教程详细讲解一下,于是便有了这篇文章. 一. ...

  10. 201621123034 《Java程序设计》第2周学习总结

    1. 本周学习总结 本周学习了基本数据类型.包装类,自动装箱与自动拆箱.数组.ArrayList.包装类可以更加方便的转换基本数据类型,而其存放的是对象的引用,而非对象本身,在对其内容进行比较时,要使 ...