作者: 秋梦无痕
链接: http://www.sinzy.net/blog/Read.asp?ID=44&BID=4371
想在VC中把GB2312/GBK字符串转换成UTF8/Unicode字符串,昨天折腾了三个多小时,在google上跑了好几圈,还是没能实现,结果今天早上一过来就搞定了。共享之:
void ConvertGBKToUtf8(CString& strGBK) {
int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL,0);
unsigned short * wszUtf8 = new unsigned short[len+1];
memset(wszUtf8, 0, len * 2 + 2);
MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, wszUtf8, len);
len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL);
char *szUtf8=new char[len + 1];
memset(szUtf8, 0, len + 1);
WideCharToMultiByte (CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL,NULL);
strGBK = szUtf8;
delete[] szUtf8;
delete[] wszUtf8;
}
void ConvertUtf8ToGBK(CString& strUtf8) {
int len=MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, NULL,0);
unsigned short * wszGBK = new unsigned short[len+1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK=new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte (CP_ACP, 0, wszGBK, -1, szGBK, len, NULL,NULL);
strUtf8 = szGBK;
delete[] szGBK;
delete[] wszGBK;
}
10/24/2006
VC中GBK与UTF8字符串的转换(zz)
订阅:
博文评论 (Atom)
2 Comments:
太感谢了,我正好拿来用!!
MultiByteToWideChar
第四个参数如果为-1返回的长度好像是包括结尾字符的,因为如果长度-1,函数会读到第三个参数结尾符号为止,请高手指点,我也不是很懂!
Post a Comment