当前位置:   article > 正文

UTF8和GBK互转_c# string gbk to utf-8

c# string gbk to utf-8
  1. string GBKToUTF8(const char* strGBK)
  2. {
  3. int len = MultiByteToWideChar(CP_ACP, 0, strGBK, -1, NULL, 0);
  4. wchar_t* wstr = new wchar_t[len + 1];
  5. memset(wstr, 0, len + 1);
  6. MultiByteToWideChar(CP_ACP, 0, strGBK, -1, wstr, len);
  7. len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
  8. char* str = new char[len + 1];
  9. memset(str, 0, len + 1);
  10. WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
  11. string strTemp = str;
  12. if (wstr) delete[] wstr;
  13. if (str) delete[] str;
  14. return strTemp;
  15. }
  1. string UTF8ToGBK(const char* strUTF8)
  2. {
  3. int nLength = MultiByteToWideChar(CP_UTF8, 0, strUTF8, -1, NULL, 0);
  4. wchar_t* wszGBK = new wchar_t[nLength + 1];
  5. memset(wszGBK, 0, (size_t)nLength * 2 + 2);
  6. MultiByteToWideChar(CP_UTF8, 0, strUTF8, -1, wszGBK, nLength);
  7. nLength = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
  8. char* szGBK = new char[nLength + 1];
  9. memset(szGBK, 0, (size_t)nLength + 1);
  10. WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, nLength, NULL, NULL);
  11. string strTemp(szGBK);
  12. if (wszGBK) delete[] wszGBK;
  13. if (szGBK) delete[] szGBK;
  14. return strTemp;
  15. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/253425
推荐阅读
相关标签
  

闽ICP备14008679号