C++ 中字符串操作--寬窄字符轉(zhuǎn)換的實(shí)例詳解
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ī)格計(jì)算"的大小。 設(shè)置 0,函數(shù)失敗; 設(shè)置 -1,函數(shù)處理整個(gè)字符串,包括/0字符串,導(dǎo)致寬字符串也會(huì)帶有/0字符,返回的長(zhǎng)度也包含/0的長(zhǎng)度; 設(shè)置 >0,根據(jù)是否包含/0,返回的結(jié)果也會(huì)相應(yīng)調(diào)整。 lpWideCharStr [out, optional]: 指向接收寬字符串的緩沖區(qū)。 cchWideChar [in]: lpWideCharStr 指向的緩沖區(qū)"以字符規(guī)格計(jì)算"的大小。 設(shè)置 0,使 lpWideCharStr 無(wú)效,并使得函數(shù)返回所需"以字符規(guī)格計(jì)算"的大小。
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); }如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
新聞熱點(diǎn)
疑難解答