Open ooolky opened 6 months ago
步骤
传输和保存时要用utf8格式,显示和绘制时要用gbk格式。
std::string GBKToUTF8(const std::string& gbk_str) { int utf8_length = MultiByteToWideChar(CP_ACP, 0, gbk_str.c_str(), -1, nullptr, 0); if (utf8_length == 0) { std::cerr << "MultiByteToWideChar failed" << std::endl; return ""; } wchar_t* utf8_buffer = new wchar_t[utf8_length]; MultiByteToWideChar(CP_ACP, 0, gbk_str.c_str(), -1, utf8_buffer, utf8_length); int utf8_bytes = WideCharToMultiByte(CP_UTF8, 0, utf8_buffer, -1, nullptr, 0, nullptr, nullptr); if (utf8_bytes == 0) { std::cerr << "WideCharToMultiByte failed" << std::endl; delete[] utf8_buffer; return ""; } char* utf8_str = new char[utf8_bytes]; WideCharToMultiByte(CP_UTF8, 0, utf8_buffer, -1, utf8_str, utf8_bytes, nullptr, nullptr); std::string result(utf8_str); delete[] utf8_buffer; delete[] utf8_str; return result; }
std::string UTF8ToGBK(const std::string& utf8_str) { // 计算需要的缓冲区大小 int wide_char_length = MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, nullptr, 0);
if (wide_char_length == 0) { std::cerr << "MultiByteToWideChar failed" << std::endl; return ""; }
wchar_t* wide_str = new wchar_t[wide_char_length];
// 转换为宽字符 if (MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, wide_str, wide_char_length) == 0) { std::cerr << "MultiByteToWideChar failed" << std::endl; delete[] wide_str; return ""; }
// 计算需要的缓冲区大小 int gbk_char_length = WideCharToMultiByte(CP_ACP, 0, wide_str, -1, nullptr, 0, nullptr, nullptr);
if (gbk_char_length == 0) { std::cerr << "WideCharToMultiByte failed" << std::endl; delete[] wide_str; return ""; }
char* gbk_str = new char[gbk_char_length];
// 转换为GBK if (WideCharToMultiByte(CP_ACP, 0, wide_str, -1, gbk_str, gbk_char_length, nullptr, nullptr) == 0) { std::cerr << "WideCharToMultiByte failed" << std::endl; delete[] wide_str; delete[] gbk_str; return ""; }
delete[] wide_str;
// 输出转换后的GBK字符串 std::string gbk_string(gbk_str); delete[] gbk_str;
return gbk_string; }
步骤
传输和保存时要用utf8格式,显示和绘制时要用gbk格式。
std::string UTF8ToGBK(const std::string& utf8_str) { // 计算需要的缓冲区大小 int wide_char_length = MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, nullptr, 0);
if (wide_char_length == 0) { std::cerr << "MultiByteToWideChar failed" << std::endl; return ""; }
wchar_t* wide_str = new wchar_t[wide_char_length];
// 转换为宽字符 if (MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, wide_str, wide_char_length) == 0) { std::cerr << "MultiByteToWideChar failed" << std::endl; delete[] wide_str; return ""; }
// 计算需要的缓冲区大小 int gbk_char_length = WideCharToMultiByte(CP_ACP, 0, wide_str, -1, nullptr, 0, nullptr, nullptr);
if (gbk_char_length == 0) { std::cerr << "WideCharToMultiByte failed" << std::endl; delete[] wide_str; return ""; }
char* gbk_str = new char[gbk_char_length];
// 转换为GBK if (WideCharToMultiByte(CP_ACP, 0, wide_str, -1, gbk_str, gbk_char_length, nullptr, nullptr) == 0) { std::cerr << "WideCharToMultiByte failed" << std::endl; delete[] wide_str; delete[] gbk_str; return ""; }
delete[] wide_str;
// 输出转换后的GBK字符串 std::string gbk_string(gbk_str); delete[] gbk_str;
return gbk_string; }