dsdafdadadf
UTF-8转GBK:
std::string UTF8ToGBK(const std::string& utf8_string) { int len = MultiByteToWideChar(CP_UTF8, 0, utf8_string.c_str(), -1, NULL, 0); std::wstring unicode_string(len, L'\0'); MultiByteToWideChar(CP_UTF8, 0, utf8_string.c_str(), -1, &unicode_string[0], len); len = WideCharToMultiByte(CP_ACP, 0, &unicode_string[0], -1, NULL, 0, NULL, NULL); std::string gbk_string(len, '\0'); WideCharToMultiByte(CP_ACP, 0, &unicode_string[0], -1, &gbk_string[0], len, NULL, NULL); return gbk_string; }
GBK转UTF-8:
std::string GBKToUTF8(const std::string& gbk_string) { int len = MultiByteToWideChar(CP_ACP, 0, gbk_string.c_str(), (int)gbk_string.size(), NULL, 0); std::wstring unicode_string(len, L'\0'); MultiByteToWideChar(CP_ACP, 0, gbk_string.c_str(), (int)gbk_string.size(), &unicode_string[0], len); len = WideCharToMultiByte(CP_UTF8, 0, &unicode_string[0], (int)unicode_string.size(), NULL, 0, NULL, NULL); std::string utf8_string(len, u8'\0'); WideCharToMultiByte(CP_UTF8, 0, &unicode_string[0], (int)unicode_string.size(), &utf8_string[0], len, NULL, NULL); return utf8_string; }