C++實(shí)現(xiàn)String與UF8互轉(zhuǎn)
UTF8_To_String
#include<Stringapiset.h> #include <iostream> std::string UTF8_To_String(const std::string& str) { int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0); wchar_t* pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然會(huì)出現(xiàn)尾巴 memset(pwBuf, 0, nwLen * 2 + 2); MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen); int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL); char* pBuf = new char[nLen + 1]; memset(pBuf, 0, nLen + 1); WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL); std::string retStr = pBuf; delete[]pBuf; delete[]pwBuf; pBuf = NULL; pwBuf = NULL; return retStr; }
String_To_UTF8
#include<Stringapiset.h> #include <iostream> std::string String_To_UTF8(const std::string& str) { int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0); wchar_t* pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然會(huì)出現(xiàn)尾巴 ZeroMemory(pwBuf, nwLen * 2 + 2); ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen); int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL); char* pBuf = new char[nLen + 1]; ZeroMemory(pBuf, nLen + 1); ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL); std::string retStr(pBuf); delete[]pwBuf; delete[]pBuf; pwBuf = NULL; pBuf = NULL; return retStr; }
到此這篇關(guān)于C++實(shí)現(xiàn)String與UF8互轉(zhuǎn)的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C++ string與int的相互轉(zhuǎn)換(使用C++11)
- C++ 字符串string和整數(shù)int的互相轉(zhuǎn)化操作
- C++中string轉(zhuǎn)換為char*類型返回后亂碼問(wèn)題解決
- C++編程之CString、string與、char數(shù)組的轉(zhuǎn)換
- C++中CString string char* char 之間的字符轉(zhuǎn)換(多種方法)
- 詳解NSString 與C++ string字符串的互轉(zhuǎn)
- C++中string與int的相互轉(zhuǎn)換實(shí)現(xiàn)代碼
- C++中將string類型轉(zhuǎn)化為int類型
- 深入理解c++中char*與wchar_t*與string以及wstring之間的相互轉(zhuǎn)換
相關(guān)文章
c++獲取進(jìn)程信息列表和進(jìn)程所調(diào)用的dll列表
這篇文章主要介紹了c++獲取進(jìn)程信息列表和進(jìn)程所調(diào)用的dll列表,大家參考使用吧2013-11-11Java3D實(shí)例之創(chuàng)建空間幾何模型的實(shí)現(xiàn)方法
本篇文章是對(duì)Java3D 創(chuàng)建空間幾何模型的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的介紹。需要的朋友參考下2013-05-05Ubuntu18.04下QT開(kāi)發(fā)Android無(wú)法連接設(shè)備問(wèn)題解決實(shí)現(xiàn)
本文主要介紹了Ubuntu18.04下QT開(kāi)發(fā)Android無(wú)法連接設(shè)備問(wèn)題解決實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06C語(yǔ)言實(shí)現(xiàn)個(gè)稅計(jì)算器
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)個(gè)稅計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10基于Matlab實(shí)現(xiàn)多目標(biāo)粘液霉菌算法的示例代碼
多目標(biāo)粘液霉菌算法(MOSMA),這是最近開(kāi)發(fā)的粘液霉菌算法(SMA)的多目標(biāo)變體,用于處理工業(yè)中的多目標(biāo)優(yōu)化問(wèn)題。本文將用Matlab實(shí)現(xiàn)這一算法,需要的可以參考一下2022-05-05如何為Qt視圖中的文字實(shí)現(xiàn)彩虹漸變效果
這篇文章主要給大家介紹了關(guān)于如何為Qt視圖中的文字實(shí)現(xiàn)彩虹漸變效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Qt具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03基于C語(yǔ)言實(shí)現(xiàn)猜數(shù)字游戲
這篇文章主要為大家詳細(xì)介紹了基于C語(yǔ)言實(shí)現(xiàn)猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05