当前位置:   article > 正文

UNICODE GBK UTF-8 编码互转(VC++)_mfc unicode转gbk

mfc unicode转gbk

1:UNICODE和GBK互转

  1. wstring MBytesToWString(const char *lpcszString)
  2. {
  3. int len = strlen(lpcszString);
  4. int unicodeLen = ::MultiByteToWideChar(CP_ACP, 0, lpcszString, - 1, NULL, 0);
  5. wchar_t *pUnicode = new wchar_t[unicodeLen + 1];
  6. memset(pUnicode, 0, (unicodeLen + 1) *sizeof(wchar_t));
  7. ::MultiByteToWideChar(CP_ACP, 0, lpcszString, - 1, (LPWSTR)pUnicode, unicodeLen);
  8. wstring wString = (wchar_t *)pUnicode;
  9. delete [] pUnicode;
  10. return wString;
  11. }
  12. string WStringToMBytes(const wchar_t *lpwcszWString)
  13. {
  14. char *pElementText;
  15. int iTextLen;
  16. // wide char to multi char
  17. iTextLen = ::WideCharToMultiByte(CP_ACP, 0, lpwcszWString, - 1, NULL, 0, NULL, NULL);
  18. pElementText = new char[iTextLen + 1];
  19. memset((void *)pElementText, 0, (iTextLen + 1) *sizeof(char));
  20. ::WideCharToMultiByte(CP_ACP, 0, lpwcszWString, 0, pElementText, iTextLen, NULL, NULL);
  21. string strReturn(pElementText);
  22. delete [] pElementText;
  23. return strReturn;
  24. }
2:GBK和UTF-8互转

  1. string GBKToUTF8(const string &strGBK)
  2. {
  3. string strOutUTF8 = "";
  4. WCHAR *str1;
  5. int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), - 1, NULL, 0);
  6. str1 = new WCHAR[n];
  7. MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), - 1, str1, n);
  8. n = WideCharToMultiByte(CP_UTF8, 0, str1, - 1, NULL, 0, NULL, NULL);
  9. char *str2 = new char[n];
  10. WideCharToMultiByte(CP_UTF8, 0, str1, - 1, str2, n, NULL, NULL);
  11. strOutUTF8 = str2;
  12. delete [] str1;
  13. str1 = NULL;
  14. delete [] str2;
  15. str2 = NULL;
  16. return strOutUTF8;
  17. }
  18. string UTF8ToGBK(const string &strUTF8)
  19. {
  20. int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), - 1, NULL, 0);
  21. WCHAR *wszGBK = new WCHAR[len + 1];
  22. memset(wszGBK, 0, (len+1)*sizeof(WCHAR));
  23. MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUTF8.c_str(), - 1, wszGBK, len);
  24. len = WideCharToMultiByte(CP_ACP, 0, wszGBK, - 1, NULL, 0, NULL, NULL);
  25. char *szGBK = new char[len + 1];
  26. memset(szGBK, 0, len + 1);
  27. WideCharToMultiByte(CP_ACP, 0, wszGBK, - 1, szGBK, len, NULL, NULL);
  28. //strUTF8 = szGBK;
  29. string strTemp(szGBK);
  30. delete [] szGBK;
  31. szGBK = NULL;
  32. delete [] wszGBK;
  33. wszGBK = NULL;
  34. return strTemp;
  35. }
3:UNICODE和UTF-8互转

  1. wstring UTF8ToWString(const char *lpcszString)
  2. {
  3. int len = strlen(lpcszString);
  4. int unicodeLen = ::MultiByteToWideChar(CP_UTF8, 0, lpcszString, - 1, NULL, 0);
  5. wchar_t *pUnicode;
  6. pUnicode = new wchar_t[unicodeLen + 1];
  7. memset((void *)pUnicode, 0, (unicodeLen + 1) *sizeof(wchar_t));
  8. ::MultiByteToWideChar(CP_UTF8, 0, lpcszString, - 1, (LPWSTR)pUnicode, unicodeLen);
  9. wstring wstrReturn(pUnicode);
  10. delete [] pUnicode;
  11. return wstrReturn;
  12. }
  13. string WStringToUTF8(const wchar_t *lpwcszWString)
  14. {
  15. char *pElementText;
  16. int iTextLen = ::WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)lpwcszWString, - 1, NULL, 0, NULL, NULL);
  17. pElementText = new char[iTextLen + 1];
  18. memset((void *)pElementText, 0, (iTextLen + 1) *sizeof(char));
  19. ::WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)lpwcszWString, - 1, pElementText, iTextLen, NULL, NULL);
  20. string strReturn(pElementText);
  21. delete [] pElementText;
  22. return strReturn;
  23. }




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

闽ICP备14008679号