正在转换xchar*std::string
converting xchar* std::string
我最近发现了一段类型为XCHAR*的c++代码。我想知道这种类型是什么,以及如何将XCHAR*转换为std::字符串。
如果有人帮忙,那将是一件非常好的事。
感谢
我有一个头文件,我使用它在UTF8(用字母"a"表示)、UTF16(用字母"w"表示)和当前构建使用的任何内容(用字母t"表示)之间进行转换。假设chris说XCHAR与TCHAR相同,那么这些函数应该可以正常工作:
inline std::string wtoa(const std::wstring& Text){
std::string s(WideCharToMultiByte(CP_UTF8, 0, Text.c_str(), Text.size(), NULL, NULL, NULL, NULL), ' ');
s.resize(WideCharToMultiByte(CP_UTF8, 0, Text.c_str(), Text.size(), &s[0], s.size(), NULL, NULL));
return s;
}
inline std::wstring atow(const std::string& Text) {
std::wstring s(MultiByteToWideChar(CP_UTF8, 0, Text.c_str(), Text.size(), NULL, NULL), ' ');
s.resize(MultiByteToWideChar(CP_UTF8, 0, Text.c_str(), Text.size(), &s[0], s.size()));
return s;
}
#ifdef _UNICODE
inline std::string ttoa(const std::wstring& Text) {return wtoa(Text);}
inline std::wstring atot(const std::string& Text) {return atow(Text);}
inline const std::wstring& ttow(const std::wstring& Text) {return Text;}
inline const std::wstring& wtot(const std::wstring& Text) {return Text;}
typedef std::wstring tstring;
typedef std::wstringstream tstringstream;
#else
inline const std::string& ttoa(const std::string& Text) {return Text;}
inline const std::string& atot(const std::string& Text) {return Text;}
inline std::wstring ttow(const std::string& Text) {return atow(Text);}
inline std::string wtot(const std::wstring& Text) {return wtoa(Text);}
typedef std::string tstring;
typedef std::stringstream tstringstream;
#endif
用法:
int main() {
XCHAR* str = ///whatever
std::string utf8 = ttoa(str);
std::wstring utf16 = ttow(str);
}
请记住,其中一些返回可变的右值,另一些返回常量的左值,但这比您在大多数代码中想象的问题要小。
相关文章:
- cppcheck在const std::string[]上引发警告
- 将std::string传递给WriteConsole API
- 为std::string的某个索引赋值
- 使用 std::string () const 函数启动线程或未来
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 如何更改大小(std::string)
- std::string 的对象真的可以移动吗?
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 无法从 std::string 中提取C++ Unicode 符号
- std::string 构造函数如何处理固定大小的 char[]?
- 真的没有来自 std::string_view 的 std::string 的显式构造函数吗?
- 将C++ std::string 转换为 UTF-16-LE 编码的字符串
- 重载 + 自己的类和 std::string 的运算符
- 如何使用 std::string 作为 QHash 的键?
- 将日语 wstring 转换为 std::string
- 可以从std::string继承以提供类型一致性吗
- 构造函数采用std::string_view与std::string并移动
- 在共享缓冲区内存中创建 ::std::string 对象
- std::string.size() 未知行为
- Valgrind 在 std::string::swap 中报告 SIGILL