国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++寬字符與普通字符的轉換實例詳解

2020-01-26 14:05:05
字體:
來源:轉載
供稿:網友

C++寬字符與普通字符的轉換實例詳解

把字符串轉換成寬字符串,

實例代碼:

wstring string2Wstring(string sToMatch) {   #ifdef _A_WIN   int iWLen = MultiByteToWideChar( CP_ACP, 0, sToMatch.c_str(), sToMatch.size(), 0, 0 ); // 計算轉換后寬字符串的長度。(不包含字符串結束符)   wchar_t *lpwsz = new wchar_t [iWLen + 1];   MultiByteToWideChar( CP_ACP, 0, sToMatch.c_str(), sToMatch.size(), lpwsz, iWLen ); // 正式轉換。   lpwsz[iWLen] = L'/0';    wstring wsToMatch(lpwsz);   delete []lpwsz; #elif _A_LINUX   setlocale( LC_CTYPE, "" ); // 很重要,沒有這一句,轉換會失敗。   int iWLen = mbstowcs( NULL, sToMatch.c_str(), sToMatch.length() ); // 計算轉換后寬字符串的長度。(不包含字符串結束符)   wchar_t *lpwsz = new wchar_t[iWLen + 1];   int i = mbstowcs( lpwsz, sToMatch.c_str(), sToMatch.length() ); // 轉換。(轉換后的字符串有結束符)   wstring wsToMatch(lpwsz);   delete []lpwsz; #endif   return wsToMatch; } //把寬字符串轉換成字符串,輸出使用 string wstring2string(wstring sToMatch) {   #ifdef _A_WIN   string sResult;   int iLen = WideCharToMultiByte( CP_ACP, NULL, sToMatch.c_str(), -1, NULL, 0, NULL, FALSE ); // 計算轉換后字符串的長度。(包含字符串結束符)   char *lpsz = new char[iLen];   WideCharToMultiByte( CP_OEMCP, NULL, sToMatch.c_str(), -1, lpsz, iLen, NULL, FALSE); // 正式轉換。   sResult.assign( lpsz, iLen - 1 ); // 對string對象進行賦值。   delete []lpsz; #elif _A_LINUX   int iLen = wcstombs( NULL, sToMatch.c_str(), 0 ); // 計算轉換后字符串的長度。(不包含字符串結束符)   char *lpsz = new char[iLen + 1];   int i = wcstombs( lpsz, sToMatch.c_str(), iLen ); // 轉換。(沒有結束符)   lpsz[iLen] = '/0';   string sResult(lpsz);   delete []lpsz; #endif   return sResult; } 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 清涧县| 扎鲁特旗| 长海县| 福海县| 浦北县| 土默特左旗| 孟州市| 交城县| 钦州市| 永康市| 革吉县| 资溪县| 闻喜县| 双桥区| 龙岩市| 宁乡县| 太谷县| 赫章县| 镇远县| 温州市| 怀化市| 安远县| 荆门市| 新沂市| 正镶白旗| 庐江县| 鱼台县| 仪征市| 崇信县| 永清县| 安庆市| 平阳县| 韩城市| 汝城县| 黄冈市| 亳州市| 怀集县| 克拉玛依市| 龙江县| 通海县| 大荔县|