先定义字符串编码的codepage值。
#define CP_GBK 936
#define CP_UTF8 65001
Utf8字符串转GBK
[复制到剪贴板] |
void Utf8Gbk(const CString& szUtf8, CString& szGbk)
{
int buffLen = 0;
int wlen = szUtf8.GetLength();
WCHAR* wbuff = new WCHAR[wlen+1];
MultiByteToWideChar(CP_UTF8, 0, szUtf8, -1, wbuff, wlen);
buffLen = WideCharToMultiByte(CP_GBK, 0, wbuff, -1, NULL, 0, 0, 0);
char* gbk = new char[buffLen+1];
WideCharToMultiByte(CP_GBK, 0, wbuff, -1, (LPSTR)gbk, buffLen, 0, 0);
delete[]wbuff;
szGbk = gbk;
delete[]gbk;
}
GBK字符串转Utf8
[复制到剪贴板] |
void GbkUtf8(const CString& szGbk, CString& szUtf8)
{
int buffLen = 0;
int wlen = szGbk.GetLength()*3;
WCHAR* wbuff = new WCHAR[wlen+1];
MultiByteToWideChar(CP_GBK, 0, szGbk, -1, wbuff, wlen);
buffLen = WideCharToMultiByte(CP_UTF8, 0, wbuff, -1, NULL, 0, 0, 0);
char* utf8 = new char[buffLen+1];
WideCharToMultiByte(CP_UTF8, 0, wbuff, -1, (LPSTR)utf8, buffLen, 0, 0);
delete[]wbuff;
szUtf8 = utf8;
delete[]utf8;
}