溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C++ 中字符串操作--寬窄字符轉(zhuǎn)換的實例詳解

發(fā)布時間:2020-10-24 10:56:03 來源:腳本之家 閱讀:131 作者:yipingg 欄目:編程語言

C++ 中字符串操作--寬窄字符轉(zhuǎn)換的實例詳解

MultiByteToWideChar

 int MultiByteToWideChar(
  _In_   UINT  CodePage,
  _In_   DWORD dwFlags,
  _In_   LPCSTR lpMultiByteStr,
  _In_   int  cbMultiByte,
  _Out_opt_ LPWSTR lpWideCharStr,
  _In_   int  cchWideChar
 );
 參數(shù)描述:
  CodePage:常用CP_ACP、CP_UTF8
  dwFlags:0
  lpMultiByteStr [in]:
    指向待轉(zhuǎn)換字符串。
  cbMultiByte [in]:
    lpMultiByteStr "以字節(jié)規(guī)格計算"的大小。
    設置 0,函數(shù)失?。?    設置 -1,函數(shù)處理整個字符串,包括\0字符串,導致寬字符串也會帶有\(zhòng)0字符,返回的長度也包含\0的長度;
    設置 >0,根據(jù)是否包含\0,返回的結(jié)果也會相應調(diào)整。
  lpWideCharStr [out, optional]:
    指向接收寬字符串的緩沖區(qū)。
  cchWideChar [in]:
    lpWideCharStr 指向的緩沖區(qū)"以字符規(guī)格計算"的大小。
    設置 0,使 lpWideCharStr 無效,并使得函數(shù)返回所需"以字符規(guī)格計算"的大小。

Code:

 int requiredBufSize = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);
 if (requiredBufSize > 0)
 {
   WCHAR *pBuffer = new WCHAR[requiredBufSize];
   MultiByteToWideChar(CP_ACP, 0, src, -1, pBuffer, requiredBufSize);
 }

WideCharToMultiByte

 int WideCharToMultiByte(
  _In_   UINT  CodePage,
  _In_   DWORD  dwFlags,
  _In_   LPCWSTR lpWideCharStr,
  _In_   int   cchWideChar,
  _Out_opt_ LPSTR  lpMultiByteStr,
  _In_   int   cbMultiByte,
  _In_opt_ LPCSTR lpDefaultChar,
  _Out_opt_ LPBOOL lpUsedDefaultChar
 );
 參數(shù)描述:
  lpDefaultChar [in, optional]:NULL
  lpUsedDefaultChar [out, optional]:NULL
  其它參數(shù)參考 MultiByteToWideChar

Code:

 int requiredBufSize = WideCharToMultiByte(CP_ACP, 0, src, -1, NULL, 0, NULL, NULL);
 if (requiredBufSize > 0)
 {
   char *pBuffer = new char[requiredBufSize];
   WideCharToMultiByte(CP_ACP, 0, src, -1, pBuffer, requiredBufSize, NULL, NULL);
 }

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI