std::string G2U(const std::string gb2312)
{
int len = MultiByteToWideChar(CP_ACP, 0, gb2312.c_str(), -1, NULL, 0);
std::vector<wchar_t> wstr(len + 1, 0);
MultiByteToWideChar(CP_ACP, 0, gb2312.c_str(), -1, wstr.data(), len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), -1, NULL, 0, NULL, NULL);
std::vector<char> str(len + 1, 0);
WideCharToMultiByte(CP_UTF8, 0, wstr.data(), -1, str.data(), len, NULL, NULL);
return std::string(str.data());
}
std::string U2G(const std::string utf8)
{
int len = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0);
std::vector<wchar_t> wstr(len + 1, 0);
MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, wstr.data(), len);
len = WideCharToMultiByte(CP_ACP, 0, wstr.data(), -1, NULL, 0, NULL, NULL);
std::vector<char> str(len + 1, 0);
WideCharToMultiByte(CP_ACP, 0, wstr.data(), -1, str.data(), len, NULL, NULL);
return std::string(str.data());
}
之前版本存在内存泄漏,现在换成std::string