CString 与 string 间的转换

CString 转 string

CString cstrTest = _T("test");
string strTest;
string = CT2A(cstrTest.GetString());

 
 
 
 
  • 1
  • 2
  • 3

string 转 CString

string strTest="test";
CString cstrTest;
cstrTest= CA2T(strTest.c_str());

 
 
 
 
  • 1
  • 2
  • 3

这里使用的方法是ATL字符串转换宏。

CT2A 、CA2T 在 Unicode 字符集下为 CW2A 、CA2W,在Muti-Byte字符集下都为 CA2A。

CW2A 将宽字符集(Unicode)转化为多字符集(ASCII)
CA2W 将多字符集(ASCII)转化为宽字符集(Unicode)

其中
C:convert
W:宽字符串,也就是 UNICODE
2:to
A:ANSI 字符串,也就是 Muti-Byte。
T : 中间类型,如果定义了 _UNICODE,则T表示W;如果定义了 _MBCS,则T表示A

具体ATL方法及注意事项参考:
yunshouhu 的 《ATL字符串转换宏》

Unicode下:

CString 转 string

CString cstrTest = _T("test");
string strTest;
string = CW2A(cstrTest.GetString());

 
 
 
 
  • 1
  • 2
  • 3

string 转 CString

string strTest="test";
CString cstrTest;
cstrTest= CA2W(strTest.c_str());

 
 
 
 
  • 1
  • 2
  • 3

多字符集下:

(没有尝试,理论上应该是)
CString 转 string

CString cstrTest = _T("test");
string strTest;
string = CA2A(cstrTest.GetString());

 
 
 
 
  • 1
  • 2
  • 3

string 转 CString

string strTest="test";
CString cstrTest;
cstrTest= CA2A(strTest.c_str());

 
 
 
 
  • 1
  • 2
  • 3
                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                </div>
</article>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值