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);  

一般如果是将 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);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值