C++中utf8字符串和gbk字符串的轉換方法
更新時間:2025年02月13日 09:05:23 作者:charlee44
文章介紹了C++中UTF-8字符串和GBK字符串之間的轉換,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
這個功能 C++ 語言本身似乎沒有標準實現,需要借助于第三方庫或者操作系統(tǒng) API。不得不吐槽一下這么重要的功能居然還沒有辦法依賴 C++ 語言本身來實現,C++ 標準委員會真是不干人事啊。那就不廢話了,直接給出 windows 下的實現。
std::string Utf8ToGbk(const std::string& utf8Str) {
// Step 1: Convert UTF-8 to Wide Char (UTF-16)
int wideCharLen =
MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, nullptr, 0);
if (wideCharLen == 0) {
throw std::runtime_error("Failed to convert from UTF-8 to wide char.");
}
std::wstring wideStr(wideCharLen, 0);
MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, &wideStr[0],
wideCharLen);
// Step 2: Convert Wide Char (UTF-16) to GBK
int gbkLen = WideCharToMultiByte(CP_ACP, 0, wideStr.c_str(), -1, nullptr, 0,
nullptr, nullptr);
if (gbkLen == 0) {
throw std::runtime_error("Failed to convert from wide char to GBK.");
}
std::string gbkStr(gbkLen, 0);
WideCharToMultiByte(CP_ACP, 0, wideStr.c_str(), -1, &gbkStr[0], gbkLen,
nullptr, nullptr);
// Remove the null terminator added by the conversion functions
gbkStr.pop_back();
return gbkStr;
}
std::string GbkToUtf8(const std::string& gbkStr) {
// Step 1: Convert GBK to Wide Char (UTF-16)
int wideCharLen =
MultiByteToWideChar(CP_ACP, 0, gbkStr.c_str(), -1, nullptr, 0);
if (wideCharLen == 0) {
throw std::runtime_error("Failed to convert from GBK to wide char.");
}
std::wstring wideStr(wideCharLen, 0);
MultiByteToWideChar(CP_ACP, 0, gbkStr.c_str(), -1, &wideStr[0], wideCharLen);
// Step 2: Convert Wide Char (UTF-16) to UTF-8
int utf8Len = WideCharToMultiByte(CP_UTF8, 0, wideStr.c_str(), -1, nullptr, 0,
nullptr, nullptr);
if (utf8Len == 0) {
throw std::runtime_error("Failed to convert from wide char to UTF-8.");
}
std::string utf8Str(utf8Len, 0);
WideCharToMultiByte(CP_UTF8, 0, wideStr.c_str(), -1, &utf8Str[0], utf8Len,
nullptr, nullptr);
// Remove the null terminator added by the conversion functions
utf8Str.pop_back();
return utf8Str;
}這段代碼的原理很簡單:
- CP_ACP 的意思就是本地編碼,就是操作系統(tǒng)系統(tǒng)定義的默認編碼,依賴于當前操作系統(tǒng)的語言和地區(qū)設置。在中文環(huán)境下就是 GBk 系列的中文編碼,例如 GB2312、GBK 或 GB18030。
- 需要使用寬字節(jié)字符串來進行中轉,在 Windows 下,std::wstring 是 16 字節(jié)字符串,使用 UTF-16 編碼。這一點有點類似于 C#的 string 和 Java 的 string,都是 UTF-16 編碼。
- MultiByteToWideChar 和 WideCharToMultiByte 都是操作系統(tǒng)的 C 接口,輸入和返回的字符串都帶'\0',因此轉到 c++ 的 string 需要去掉最后的'\0'字符。這一點需要注意。
測試了用例沒有問題。測試 Utf8ToGbk:
// string utfStr = u8"這是一個測試的中文字符串,檢查一下"; // string utfStr = u8"測試"; string utfStr = u8"abcdefg"; string gbkStr = Utf8ToGbk(utfStr); // cout << gbkStr << "-------" << endl; // cout << gbkStr.length() << endl; // cout << gbkStr.c_str() << endl; // cout << strlen(gbkStr.c_str()) << endl;
測試 GbkToUtf8:
#ifdef _WIN32 SetConsoleOutputCP(65001); #endif // string gbkStr = "測試"; string gbkStr = "這是一個測試的中文字符串,檢查一下"; // string gbkStr = "abcdefg"; cout << gbkStr.length() << endl; string utfStr = GbkToUtf8(gbkStr); cout << utfStr << endl; cout << utfStr.length() << endl;
以上是 Windows 的實現,Linux 環(huán)境要使用別的辦法,例如使用 iconv 庫。
到此這篇關于c++中utf8字符串和gbk字符串的轉換的文章就介紹到這了,更多相關c++ utf8字符串和gbk字符串轉換內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

