C++中utf8字符串和gbk字符串的轉(zhuǎn)換方法
這個(gè)功能 C++ 語(yǔ)言本身似乎沒(méi)有標(biāo)準(zhǔn)實(shí)現(xiàn),需要借助于第三方庫(kù)或者操作系統(tǒng) API。不得不吐槽一下這么重要的功能居然還沒(méi)有辦法依賴 C++ 語(yǔ)言本身來(lái)實(shí)現(xiàn),C++ 標(biāo)準(zhǔn)委員會(huì)真是不干人事啊。那就不廢話了,直接給出 windows 下的實(shí)現(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; }
這段代碼的原理很簡(jiǎn)單:
- CP_ACP 的意思就是本地編碼,就是操作系統(tǒng)系統(tǒng)定義的默認(rèn)編碼,依賴于當(dāng)前操作系統(tǒng)的語(yǔ)言和地區(qū)設(shè)置。在中文環(huán)境下就是 GBk 系列的中文編碼,例如 GB2312、GBK 或 GB18030。
- 需要使用寬字節(jié)字符串來(lái)進(jìn)行中轉(zhuǎn),在 Windows 下,std::wstring 是 16 字節(jié)字符串,使用 UTF-16 編碼。這一點(diǎn)有點(diǎn)類似于 C#的 string 和 Java 的 string,都是 UTF-16 編碼。
- MultiByteToWideChar 和 WideCharToMultiByte 都是操作系統(tǒng)的 C 接口,輸入和返回的字符串都帶'\0',因此轉(zhuǎn)到 c++ 的 string 需要去掉最后的'\0'字符。這一點(diǎn)需要注意。
測(cè)試了用例沒(méi)有問(wèn)題。測(cè)試 Utf8ToGbk:
// string utfStr = u8"這是一個(gè)測(cè)試的中文字符串,檢查一下"; // string utfStr = u8"測(cè)試"; 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;
測(cè)試 GbkToUtf8:
#ifdef _WIN32 SetConsoleOutputCP(65001); #endif // string gbkStr = "測(cè)試"; string gbkStr = "這是一個(gè)測(cè)試的中文字符串,檢查一下"; // string gbkStr = "abcdefg"; cout << gbkStr.length() << endl; string utfStr = GbkToUtf8(gbkStr); cout << utfStr << endl; cout << utfStr.length() << endl;
以上是 Windows 的實(shí)現(xiàn),Linux 環(huán)境要使用別的辦法,例如使用 iconv 庫(kù)。
到此這篇關(guān)于c++中utf8字符串和gbk字符串的轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)c++ utf8字符串和gbk字符串轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++實(shí)現(xiàn)utf8字符串和gbk字符串互轉(zhuǎn)
- C++實(shí)現(xiàn)判斷一個(gè)字符串是否為UTF8或GBK格式的方法
- C/C++實(shí)現(xiàn)數(shù)字與字符串互相轉(zhuǎn)換的多種方法
- C++中jsoncpp庫(kù)和nlohmann-json庫(kù)實(shí)現(xiàn)JSON與字符串類型轉(zhuǎn)換
- 在C++中把字符串轉(zhuǎn)換為整數(shù)的兩種簡(jiǎn)單方法
- C++實(shí)現(xiàn)將長(zhǎng)整型數(shù)轉(zhuǎn)換為字符串的示例代碼
- C++實(shí)現(xiàn)十六進(jìn)制字符串轉(zhuǎn)換成int整形值的示例
- c++中數(shù)字與字符串之間的轉(zhuǎn)換方法(推薦)
- C++實(shí)現(xiàn)十六進(jìn)制字符串轉(zhuǎn)換為十進(jìn)制整數(shù)的方法
相關(guān)文章
C語(yǔ)言Turbo C下實(shí)現(xiàn)俄羅斯方塊
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言Turbo C下寫(xiě)的俄羅斯方塊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02如何在C語(yǔ)言中提取Shellcode并執(zhí)行
Shellcode是一種獨(dú)立于應(yīng)用程序的機(jī)器代碼,通常用于實(shí)現(xiàn)特定任務(wù),如執(zhí)行遠(yuǎn)程命令、注入惡意軟件或利用系統(tǒng)漏洞,本文將深入探討如何在C語(yǔ)言中提取Shellcode,并通過(guò)XOR加密技術(shù)增加其混淆程度,文中通過(guò)代碼示例講解的非常詳細(xì),需要的朋友可以參考下2023-12-12C++編寫(xiě)生成不重復(fù)的隨機(jī)數(shù)代碼
本文給大家匯總介紹了3種c++實(shí)現(xiàn)生成不重復(fù)的隨機(jī)數(shù)的函數(shù),十分的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-05-05C語(yǔ)言數(shù)組按協(xié)議存儲(chǔ)與按協(xié)議解析數(shù)據(jù)的實(shí)現(xiàn)
今天小編就為大家分享一篇關(guān)于C語(yǔ)言數(shù)組按協(xié)議存儲(chǔ)與按協(xié)議解析數(shù)據(jù)的實(shí)現(xiàn),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12C語(yǔ)言中設(shè)置進(jìn)程優(yōu)先順序的方法
這篇文章主要介紹了C語(yǔ)言中設(shè)置進(jìn)程優(yōu)先順序的方法,包括setpriority()函數(shù)和getpriority()函數(shù)以及nice()函數(shù),需要的朋友可以參考下2015-08-08