CString与其他类型的转换----CString字符串转换续

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

posted on 2008-01-12 23:00 李亚 阅读(3762) 评论(0)  编辑 收藏 引用 所属分类: MFC/VC


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

公告

这世界并不会在意你的自尊,这世界指望你在自我感觉良好之前先要有所成就!

常用链接

留言簿(3)

随笔分类(32)

随笔档案(32)

相册

最新随笔

搜索

最新评论

阅读排行榜

评论排行榜