utf8转gbk,libcurl中文乱码处理

时间:2022-04-17 00:03:46

这两个转码在网页客户端处理用很常见,所使用的平台为VS2010,字符集采用多字节字符集

utf8转gbk

string UTF8ToGBK(const std::string& strUTF8)
{
int len = MultiByteToWideChar(CP_UTF8, , strUTF8.c_str(), -, NULL, );
WCHAR* wszGBK = new WCHAR[len+];
memset(wszGBK, , len * + );
MultiByteToWideChar(CP_UTF8, , (LPCTSTR)strUTF8.c_str(), -, wszGBK, len); len = WideCharToMultiByte(CP_ACP, , wszGBK, -, NULL, , NULL, NULL);
char *szGBK = new char[len + ];
memset(szGBK, , len + );
WideCharToMultiByte(CP_ACP,, wszGBK, -, szGBK, len, NULL, NULL);
std::string strTemp(szGBK);
delete[]szGBK;
delete[]wszGBK;
return strTemp;
}

gbk转utf8

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