MFC:UNICODE下CString与char* ,string类型

小菜初用MFC,就被MFC中各种类型相互转换给难住了,查了网上的一些资料,现总结如下:


Unicode下 CString 转换为 string 类型:

CString s1=L"hello";
string s2=CT2A(s1.GetBuffer());//方法一 
string s2( CW2A( s1.GetString() ) ); //方法二 
CT2CA pszName(s1); string s2(pszName);//方法三 
wstring ws(s1); string s2;  s2.assign(ws.begin(), ws.end());//方法四
//需要声明命名空间:using namespace std;

Unicode下 string 转换为 CString 类型:

string s1="hello";
CString s2=CString(s1.c_str());

Unicode下 CString 转换为 char* 类型:

//使用函数:T2A、W2A
CString s = _T("hello");
//声明标识符 
USES_CONVERSION;//必须要有
//调用函数,T2A和W2A均支持ATL和MFC中的字符转换 
char * c = T2A(s);    
//char * c = W2A(s); //也可实现转换
//注意:需要添加引用#include <atlconv.h>

相应的,Unicode下 char* 转换为 CString 需用函数A2T、W2T:

char *c="hello";
USES_CONVERSION;
CString s=A2T(c);
//CString s=A2W(c);
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值