VC++中CString、char *之間的相互轉換[Unicode設定下]
VC++中CString、char *之間的相互轉換[Unicode設定下]
CString –>char *
CString str1 =_T(“123”); int len = WideCharToMultiByte(CP_ACP,0,str1,-1,NULL,0,NULL,NULL); char *ptxtTemp = new char[len +1]; WideCharToMultiByte(CP_ACP,0,str1,-1,ptxtTemp,len,NULL,NULL ); //… delete[] ptxtTemp;
|
char * –>CString
char *p =”test”;//方法一 CString str(p); |
char * pFileName = “你好,世界!Hello,World”; //計算char *陣列大小,以位元組為單位,一個漢字占兩個位元組 int charLen = strlen(pFileName); //計算多位元組字元的大小,按字元計算。 int len = MultiByteToWideChar(CP_ACP,0,pFileName,charLen,NULL,0); //為寬位元組字元陣列申請空間,陣列大小為按位元組計算的多位元組字元大小 TCHAR *buf = new TCHAR[len + 1]; //多位元組編碼轉換成寬位元組編碼 MultiByteToWideChar(CP_ACP,0,pFileName,charLen,buf,len); buf[len] = ‘\0‘; //添加字串結尾,注意不是len+1 //將TCHAR陣列轉換為CString CString pWideChar; pWideChar.Append(buf); //刪除緩衝區 delete []buf; |