CString 与其他类型的转换


1、字符串与数的转换:
atof(字 符串->double, int, long), itoa(int->字符串), ltoa(long int->字符串)
double->CString 的方法:CString::Format("%d", &dX);
 
2、CString to char*
//经过类 型强制转换,可以将CString类型转换成char*,例如:
CString cStr = "Hello,world!";
char *zStr = (char*)(LPCTSTR)cStr;
当然,这只是最快捷的一种方法。因为CString自带一个函数可以将 CString对象转换成const char*,也就是LPCTSTR。

3、char* to CString
//char* 类型可以直接给CString,完成自动转换,例如:
char *zStr = "Hello,world!";
CString cStr = zStr;

4、CString to LPCSTR
//将CString转换成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);
 


原文地址:https://www.cnblogs.com/chinasasu/p/1715691.html