Wal*_*oni 12 c++ string mfc visual-studio
我有一个std::string.我需要将其转换std:string为Cstring.
我尝试使用.c_str()但它只用于非unicode项目,我使用unicode项目(因为非unicode项目与VS2013一起被贬低).
任何人都可以告诉我如何转换std::string为CStringunicode项目?
IIn*_*ble 31
CString有一个带const char*(CStringT::CStringT)的转换构造函数.将a转换std::string为a CString非常简单:
std::string stdstr("foo");
CString cstr(stdstr.c_str());
Run Code Online (Sandbox Code Playgroud)
这适用于UNICODE和MBCS项目.如果std::string包含嵌入的NUL字符,则必须使用带有length参数的转换构造函数:
std::string stdstr("foo");
stdstr += '\0';
stdstr += "bar";
CString cstr(stdstr.c_str(), stdstr.length());
Run Code Online (Sandbox Code Playgroud)
请注意,转换构造函数隐式使用当前线程(CP_THREAD_ACP)的ANSI代码页来转换ANSI和UTF-16编码.如果您不能(或不想)更改线程的ANSI代码页,但仍需要指定用于转换的显式代码页,则必须使用其他解决方案(例如ATL和MFC字符串转换宏).
使用 ATL 转换宏。当您使用 CString 时,它们在任何情况下都适用。CString 是 MBCS 或 Unicode...取决于您的编译器设置。
std::string str = "string";
CString ss(CA2T(str.c_str());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53357 次 |
| 最近记录: |