现在的位置: 首页 > 综合 > 正文

CString及string,char *与其他数据类型的转换和操作 .

2019年10月08日 ⁄ 综合 ⁄ 共 459字 ⁄ 字号 评论关闭

(2)转换:

●数学类型与CString相互转化

数学类型转化为CString

可用Format函数,举例:

CString s;

int i = 64;

s.Format("%d", i)

CString转换为数学类型:举例
CString strValue("1.234");

double dblValue;

dblValue = atof((LPCTSTR)strValue);

●CString与char*相互转换举例

CString strValue(“Hello”);

char *szValue;

szValue=strValue.GetBuffer(szValue);

也可用(LPSTR)(LPCTSTR)对CString//  进行强制转换. 

szValue=(LPSTR)(LPCTSTR)strValue;

反过来可直接赋值:

char *szChar=NULL;

CString strValue;

szChar=new char[10];

memset(szChar,0,10);

strcpy(szChar,”Hello”);

strValue=szChar;

抱歉!评论已关闭.