C++ 中字符串操作--寬窄字符轉換的實例詳解
MultiByteToWideChar
int MultiByteToWideChar( _In_ UINT CodePage, _In_ DWORD dwFlags, _In_ LPCSTR lpMultiByteStr, _In_ int cbMultiByte, _Out_opt_ LPWSTR lpWideCharStr, _In_ int cchWideChar ); 參數描述: CodePage:常用CP_ACP、CP_UTF8 dwFlags:0 lpMultiByteStr [in]: 指向待轉換字符串。 cbMultiByte [in]: lpMultiByteStr "以字節規格計算"的大小。 設置 0,函數失??; 設置 -1,函數處理整個字符串,包括\0字符串,導致寬字符串也會帶有\0字符,返回的長度也包含\0的長度; 設置 >0,根據是否包含\0,返回的結果也會相應調整。 lpWideCharStr [out, optional]: 指向接收寬字符串的緩沖區。 cchWideChar [in]: lpWideCharStr 指向的緩沖區"以字符規格計算"的大小。 設置 0,使 lpWideCharStr 無效,并使得函數返回所需"以字符規格計算"的大小。
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 ); 參數描述: lpDefaultChar [in, optional]:NULL lpUsedDefaultChar [out, optional]:NULL 其它參數參考 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); }
如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。