分类: C/C++
2009-06-05 13:55:44
vc中常用的几个数据转换方法-int char* float与CString 之间的转换
1、int <->CString
1) int ->CString
int n = 1;
CString str;
str.Format("%d",n);
2) CString->int
CString str = "1";
int n = atoi(str.GetBuffer(0));
2. char* 与CString
1)char*->CString
char sz[128];
CString str;
str.Format("%s",sz);
2) CString -> char*
CString str;
int nLength = str.GetLength();
char* sz = new char[nLength];
sz = str.GetBuffer(0);
有人说这里有错误!会造成内存泄露;
char* sz = new char[nLength];
sz = str.GetBuffer(0);
应改为:
char* sz = str.GetBuffer(0);
3. float<->CString
1)float->CString
float f = 0.0;
CString str;
str.Format("%f",f);
2) CString->float
CString str = "0.0";
float f = atof(str.GetBuffer(0));