Add utf8 to gbk function.

This commit is contained in:
luocai 2024-06-13 10:08:09 +08:00
parent 2ad8209814
commit 32c2c860d9
2 changed files with 21 additions and 0 deletions

View File

@ -96,5 +96,25 @@ slow:
return true; return true;
} }
std::string UTF8ToGBK(const std::string &utf8Str) {
int wideStrLen = MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, NULL, 0);
if (wideStrLen == 0) {
return "";
}
std::wstring wideStr(wideStrLen, 0);
MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, &wideStr[0], wideStrLen);
int gbkStrLen = WideCharToMultiByte(CP_ACP, 0, wideStr.c_str(), -1, NULL, 0, NULL, NULL);
if (gbkStrLen == 0) {
return "";
}
std::string gbkStr(gbkStrLen, 0);
WideCharToMultiByte(CP_ACP, 0, wideStr.c_str(), -1, &gbkStr[0], gbkStrLen, NULL, NULL);
return gbkStr;
}
} // namespace StringUtility } // namespace StringUtility
} // namespace Amass } // namespace Amass

View File

@ -61,6 +61,7 @@ std::string_view utf8At(const std::string &text, size_t index);
size_t utf8CharacterByteSize(const char *character); size_t utf8CharacterByteSize(const char *character);
std::wstring stringToWString(const std::string &string); std::wstring stringToWString(const std::string &string);
std::string wstringToString(const std::wstring &string); std::string wstringToString(const std::wstring &string);
std::string UTF8ToGBK(const std::string &utf8Str);
} // namespace StringUtility } // namespace StringUtility