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);
一般如果是将 int 转换成 CString 类型,一般就是用 Format 函数方法如下
CString Str;
int a = 15;
Str.Format("%d",a);
如果是将 int 转换为 char* 类型就需要用到 _itoa 这个函数方法如下
char chr[1024]={0};
int a = 15;
_itoa(a,chr,10);