C++中utf8字符串和gbk字符串的轉(zhuǎn)換方法
這個功能 C++ 語言本身似乎沒有標準實現(xiàn),需要借助于第三方庫或者操作系統(tǒng) API。不得不吐槽一下這么重要的功能居然還沒有辦法依賴 C++ 語言本身來實現(xiàn),C++ 標準委員會真是不干人事啊。那就不廢話了,直接給出 windows 下的實現(xiàn)。
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ū)設(shè)置。在中文環(huán)境下就是 GBk 系列的中文編碼,例如 GB2312、GBK 或 GB18030。
- 需要使用寬字節(jié)字符串來進行中轉(zhuǎn),在 Windows 下,std::wstring 是 16 字節(jié)字符串,使用 UTF-16 編碼。這一點有點類似于 C#的 string 和 Java 的 string,都是 UTF-16 編碼。
- MultiByteToWideChar 和 WideCharToMultiByte 都是操作系統(tǒng)的 C 接口,輸入和返回的字符串都帶'\0',因此轉(zhuǎn)到 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 的實現(xiàn),Linux 環(huán)境要使用別的辦法,例如使用 iconv 庫。
到此這篇關(guān)于c++中utf8字符串和gbk字符串的轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)c++ utf8字符串和gbk字符串轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++實現(xiàn)utf8字符串和gbk字符串互轉(zhuǎn)
- C++實現(xiàn)判斷一個字符串是否為UTF8或GBK格式的方法
- C/C++實現(xiàn)數(shù)字與字符串互相轉(zhuǎn)換的多種方法
- C++中jsoncpp庫和nlohmann-json庫實現(xiàn)JSON與字符串類型轉(zhuǎn)換
- 在C++中把字符串轉(zhuǎn)換為整數(shù)的兩種簡單方法
- C++實現(xiàn)將長整型數(shù)轉(zhuǎn)換為字符串的示例代碼
- C++實現(xiàn)十六進制字符串轉(zhuǎn)換成int整形值的示例
- c++中數(shù)字與字符串之間的轉(zhuǎn)換方法(推薦)
- C++實現(xiàn)十六進制字符串轉(zhuǎn)換為十進制整數(shù)的方法
相關(guān)文章
C++?OpenCV實現(xiàn)之實現(xiàn)紅綠燈識別
本文以實現(xiàn)行車過程當中的紅綠燈識別為目標,核心的內(nèi)容包括:OpenCV輪廓識別原理以及OpenCV紅綠燈識別的實現(xiàn)具體步驟,感興趣的可以了解一下2022-08-08
C語言數(shù)據(jù)結(jié)構(gòu)之線索二叉樹及其遍歷
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)之線索二叉樹及其遍歷的相關(guān)資料,為了加快查找節(jié)點的前驅(qū)和后繼。對二叉樹的線索化就是對二叉樹進行一次遍歷,在遍歷的過程中檢測節(jié)點的左右指針是否為空,如果是空,則將他們改為指向前驅(qū)和后繼節(jié)點的線索,需要的朋友可以參考下2017-08-08
C++實現(xiàn)shared_ptr共享指針的示例代碼
本文主要介紹了C++實現(xiàn)shared_ptr共享指針的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2026-03-03

