CString UTF8ToGB2312(CString str)
{
int len;
// UTF8转换成Unicode
len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
wchar_t *pUnicode = new wchar_t[len+1];
memset(pUnicode, 0, (len+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, str, -1, (LPWSTR)pUnicode, len);
// Unicode转换成GB2312
len = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)pUnicode, -1, NULL, 0, NULL, NULL);
CHAR *pTarget = new CHAR[len+1];
memset(pTarget, 0, len+1);
WideCharToMultiByte(CP_ACP, 0, (LPWSTR)pUnicode, -1, pTarget, len, NULL, NULL);
CString rt;
rt.Format("%s",pTarget);
delete []pUnicode;
delete []pTarget;
return rt;
}
CString GB2312ToUTF8(CString str)
{
int len;
// GB2312转换成Unicode
len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
wchar_t *pUnicode = new wchar_t[len+1];
memset(pUnicode, 0, (len+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0, str, -1, (LPWSTR)pUnicode, len);
// Unicode转换成UTF8
len = WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)pUnicode, -1, NULL, 0, NULL, NULL);
CHAR *pTarget = new CHAR[len+1];
memset(pTarget, 0, len+1);
WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)pUnicode, -1, pTarget, len, NULL, NULL);
CString rt;
rt.Format("%s",pTarget);
delete []pUnicode;
delete []pTarget;
return rt;
}
MultiByteToWideChar和WideCharToMultiByte,码迷,mamicode.com