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


转自:http://blog.csdn.net/sunflover454/article/details/50436571


1:UNICODE和GBK互转

[cpp]  view plain  copy
  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.   
  13. string WStringToMBytes(const wchar_t *lpwcszWString)  
  14. {  
  15.     char *pElementText;  
  16.     int iTextLen;  
  17.     // wide char to multi char  
  18.     iTextLen = ::WideCharToMultiByte(CP_ACP, 0, lpwcszWString,  - 1, NULL, 0, NULL, NULL);  
  19.     pElementText = new char[iTextLen + 1];  
  20.     memset((void *)pElementText, 0, (iTextLen + 1) *sizeof(char));  
  21.     ::WideCharToMultiByte(CP_ACP, 0, lpwcszWString, 0, pElementText, iTextLen, NULL, NULL);  
  22.     string strReturn(pElementText);  
  23.     delete [] pElementText;  
  24.     return strReturn;  
  25. }  
2:GBK和UTF-8互转

[cpp]  view plain  copy
  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.   
  19. string UTF8ToGBK(const string &strUTF8)  
  20. {  
  21.     int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(),  - 1, NULL, 0);  
  22.     WCHAR *wszGBK = new WCHAR[len + 1];  
  23.     memset(wszGBK, 0, (len+1)*sizeof(WCHAR));  
  24.     MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUTF8.c_str(),  - 1, wszGBK, len);  
  25.     len = WideCharToMultiByte(CP_ACP, 0, wszGBK,  - 1, NULL, 0, NULL, NULL);  
  26.     char *szGBK = new char[len + 1];  
  27.     memset(szGBK, 0, len + 1);  
  28.     WideCharToMultiByte(CP_ACP, 0, wszGBK,  - 1, szGBK, len, NULL, NULL);  
  29.     //strUTF8 = szGBK;  
  30.     string strTemp(szGBK);  
  31.     delete [] szGBK;  
  32.     szGBK = NULL;  
  33.     delete [] wszGBK;  
  34.     wszGBK = NULL;  
  35.     return strTemp;  
  36. }  
3:UNICODE和UTF-8互转

[cpp]  view plain  copy
  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.   
  14. string WStringToUTF8(const wchar_t *lpwcszWString)  
  15. {  
  16.     char *pElementText;  
  17.     int iTextLen = ::WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)lpwcszWString,  - 1, NULL, 0, NULL, NULL);  
  18.     pElementText = new char[iTextLen + 1];  
  19.     memset((void *)pElementText, 0, (iTextLen + 1) *sizeof(char));  
  20.     ::WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)lpwcszWString,  - 1, pElementText, iTextLen, NULL, NULL);  
  21.     string strReturn(pElementText);  
  22.     delete [] pElementText;  
  23.     return strReturn;  
  24. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值