CString與其他類型的轉(zhuǎn)換
這里只是介紹了很常見方法,還有其他方法也可以實現(xiàn)相關(guān)的功能!
1、字符串與數(shù)的轉(zhuǎn)換:
atof(字符串->double,int,long),itoa(int->字符串),ltoa(long int->字符串)
double->CString的方法:CString::Format("%d", &dX);
2、CString to char*//經(jīng)過類型強制轉(zhuǎn)換,可以將CString類型轉(zhuǎn)換成char*,例如:
CString cStr = "Hello,world!";
char *zStr = (char*)(LPCTSTR)cStr;
當(dāng)然,這只是最快捷的一種方法。因為CString自帶一個函數(shù)可以將CString對象轉(zhuǎn)換成const char*,也就是LPCTSTR。
3、char* to CString//char*類型可以直接給CString,完成自動轉(zhuǎn)換,例如:
char *zStr = "Hello,world!";
CString cStr = zStr;
4、CString to LPCSTR //將CString轉(zhuǎn)換成LPCSTR,需要獲得CString的長度CString cStr=_T("Hello,world!");
int nLen = cStr.GetLength();
LPCSTR lpszBuf = cStr.GetBuffer(nLen);
5、CString to LPSTR//這個和第4個技巧是一樣的,例如:
CString cStr = _T("Hello,world!");
int nLen = str.GetLength();
LPSTR lpszBuf = str.GetBuffer(nLen);