string UTF8ToANSI(const char* str)
{
string res = "";
wchar_t* src;
LPSTR lpres;
int tmp = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
src = new wchar_t[tmp + 5];
MultiByteToWideChar(CP_UTF8, 0, str, -1, src, tmp);
tmp = WideCharToMultiByte(CP_ACP, 0, src, -1, NULL, 0, NULL, NULL);
lpres = new char[tmp + 5];
WideCharToMultiByte(CP_ACP, 0, src, -1, lpres, tmp, NULL, NULL);
res = lpres;
delete[]src;
delete[]lpres;
return res;
}
string GbkToUtf8(const char* src_str)
{
int len = MultiByteToWideChar(CP_ACP, 0, src_str, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_ACP, 0, src_str, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
string strTemp = str;
if (wstr) delete[] wstr;
if (str) delete[] str;
return strTemp;
}
string Utf8ToGbk(const char* src_str)
{
int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, NULL, 0);
wchar_t* wszGBK = new wchar_t[len + 1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, src_str, -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);
string strTemp(szGBK);
if (wszGBK) delete[] wszGBK;
if (szGBK) delete[] szGBK;
return strTemp;
}
C++乱码转为UTF-8,GBKToUTF8, UTF8ToGBK
于 2024-05-30 13:40:04 首次发布