Zi 字媒體
2017-07-25T20:27:27+00:00
VC++中CString、char *之間的相互轉換[Unicode設定下]
資料來源:http://blog.jasonding.top/2015/01/30/Programming/【编程拾遗】LPCTSTR、CString、char、string之间的相互转换/
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;
寫了
5860316篇文章,獲得
23313次喜歡