版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
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>