宽字节字符集(Unicode)、多字节字符集(Multi-Byte) 自适应

CString 转 string


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

string 转 CString

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

这里使用的方法是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());

string 转 CString

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

多字符集下:
(没有尝试,理论上应该是)
CString 转 string

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

string 转 CString

扫描二维码关注公众号,回复: 11530098 查看本文章
string strTest="test";
CString cstrTest;
cstrTest= CA2A(strTest.c_str());

猜你喜欢

转载自blog.csdn.net/qq_23350817/article/details/103633802