在Windows編程中,經(jīng)常會(huì)碰到字符串之間的轉(zhuǎn)換,char*轉(zhuǎn)LPCWSTR也是其中一個(gè)比較常見(jiàn)的轉(zhuǎn)換。下面就列出幾種比較常用的轉(zhuǎn)換方法。
1、通過(guò) MultiByteToWideChar 函數(shù)轉(zhuǎn)換
MultiByteToWideChar函數(shù)是將多字節(jié)轉(zhuǎn)換為寬字節(jié)的一個(gè)API函數(shù),它的原型如下:
int MultiByteToWideChar(
UINT CodePage, // code page
DWord dwFlags, // character-type options
LPCSTR lpMultiByteStr, // string to map
int cbMultiByte, // number of bytes in string
LPWSTR lpWideCharStr, // wide-character buffer
int cchWideChar // size of buffer
);
LPCWSTR實(shí)際上也是CONST WCHAR *類(lèi)型
char* szStr = "測(cè)試字符串";
WCHAR wszClassName[256];
memset(wszClassName,0,sizeof(wszClassName));
MultiByteToWideChar(CP_ACP,0,szStr,strlen(szStr)+1,wszClassName,
sizeof(wszClassName)/sizeof(wszClassName[0]));
2、通過(guò) T2W 轉(zhuǎn)換宏
char* szStr = "測(cè)試字符串";
CString str = CString(szStr);
USES_CONVERSION;
LPCWSTR wszClassName = new WCHAR[str.GetLength()+1];
wcscpy((LPTSTR)wszClassName,T2W((LPTSTR)str.GetBuffer(NULL)));
str.ReleaseBuffer();
3、通過(guò) A2CW 轉(zhuǎn)換
char* szStr = "測(cè)試字符串";
CString str = CString(szStr);
USES_CONVERSION;
LPCWSTR wszClassName = A2CW(W2A(str));
str.ReleaseBuffer();
上述方法都是UniCode環(huán)境下測(cè)試的。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注