// 将字符串从 UTF-8 编码转换到 GBK 编码
string UTF8ToGBK(string utf8)
{
int len = (int)utf8.size();
int u_len = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), len, NULL, 0);
vector<WCHAR> unicode_str(u_len);
WCHAR* u_str = &unicode_str.front();
MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), len, u_str, u_len);
int g_len = WideCharToMultiByte(936, 0, u_str, u_len, NULL, 0, NULL, NULL);
vector<char> gbk_str(g_len);
char* g_str = &gbk_str.front();
WideCharToMultiByte(936, 0, u_str, u_len, g_str, g_len, NULL, NULL);
return string(gbk_str.begin(), gbk_str.end());
}
|