C++ 中字符串操作--寬窄字符轉換的實例詳解
更新時間:2017年09月17日 08:59:47 作者:yipingg
這篇文章主要介紹了C++ 中字符串操作--寬窄字符轉換的實例詳解的相關資料,希望通過本文能幫助到大家實現(xiàn)這樣的功能更,需要的朋友可以參考下
C++ 中字符串操作--寬窄字符轉換的實例詳解
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]: 指向待轉換字符串。 cbMultiByte [in]: lpMultiByteStr "以字節(jié)規(guī)格計算"的大小。 設置 0,函數(shù)失?。? 設置 -1,函數(shù)處理整個字符串,包括\0字符串,導致寬字符串也會帶有\(zhòng)0字符,返回的長度也包含\0的長度; 設置 >0,根據(jù)是否包含\0,返回的結果也會相應調(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ū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:
相關文章
c++中l(wèi)og4cplus日志庫使用的基本步驟和示例代碼
這篇文章主要給大家介紹了關于c++中l(wèi)og4cplus日志庫使用的相關資料,log4cplus是一款開源的c++日志庫,具有線程安全,靈活,以及多粒度控制的特點,log4cplus可以將日志按照優(yōu)先級進行劃分,使其可以面向程序的調(diào)試,運行,測試,后期維護等軟件全生命周期,需要的朋友可以參考下2024-06-06