码迷,mamicode.com
首页 > 其他好文 > 详细

UTF8-GBK WideCharToMultiByte MultiByteToWideChar

时间:2014-05-09 23:05:27      阅读:461      评论:0      收藏:0      [点我收藏+]

标签:code   c   int   string   art   set   

//MFC版本

CString UTF8ToGBK(const CString& strUTF8)
{
//确定转换为Unicode需要多少缓冲区(返回值也包含了最后一个NULL字符)
int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8, -1, NULL, 0);
unsigned short * wszGBK = new unsigned short[len + 1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUTF8, -1, (LPWSTR)wszGBK, len);

len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK = new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte(CP_ACP,0, (LPCWSTR)wszGBK, -1, szGBK, len, NULL, NULL);
CString gbkCString(szGBK);
delete[]szGBK;
delete[]wszGBK;
return gbkCString;
}

//C++标准版本

std::string GBKToUTF8(const std::string& strGBK)
{
 std::string strOutUTF8 = "";
 WCHAR * str1;
 int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
 str1 = new WCHAR[n];
 MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);
 n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
 char * str2 = new char[n];
 WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
 strOutUTF8 = str2;
 delete[]str1;
 str1 = NULL;
 delete[]str2;
 str2 = NULL;
 return strOutUTF8;
}

UTF8-GBK WideCharToMultiByte MultiByteToWideChar,布布扣,bubuko.com

UTF8-GBK WideCharToMultiByte MultiByteToWideChar

标签:code   c   int   string   art   set   

原文地址:http://www.cnblogs.com/coolbear/p/3718892.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!