如何将TCHAR
数组转换为std::string
(不std::basic_string
)?如何将TCHAR数组转换为std :: string?
回答
TCHAR
只是一个typedef,根据您的编译配置,默认为char
或wchar
。
标准模板库支持ASCII(使用std::string
)和宽字符集(使用std::wstring
)。所有你需要做的是typedef字符串作为std :: string或std :: wstring,具体取决于你的编译配置。为了保持灵活性,你可以使用下面的代码:
#ifndef UNICODE
typedef std::string String;
#else
typedef std::wstring String;
#endif
现在你可以在你的代码中使用String
,让编译器处理讨厌的部分。字符串现在将具有构造函数,可让您将TCHAR
转换为std::string
或std::wstring
。
TCHAR类型是char
或wchar_t
,具体取决于您的项目设置。
#ifdef UNICODE
// TCHAR type is wchar_t
#else
// TCHAR type is char
#endif
所以,如果你必须使用std::string
代替std::wstring
,你应该使用一个转换功能。我可能会使用wcstombs
或WideCharToMultiByte
。
TCHAR * text;
#ifdef UNICODE
/*/
// Simple C
const size_t size = (wcslen(text) + 1) * sizeof(wchar_t);
wcstombs(&buffer[0], text, size);
std::vector<char> buffer(size);
/*/
// Windows API (I would use this)
std::vector<char> buffer;
int size = WideCharToMultiByte(CP_UTF8, 0, text, -1, NULL, 0, NULL, NULL);
if (size > 0) {
buffer.resize(size);
WideCharToMultiByte(CP_UTF8, 0, text, -1, static_cast<BYTE*>(&buffer[0]), buffer.size(), NULL, NULL);
}
else {
// Error handling
}
//*/
std::string string(&buffer[0]);
#else
std::string string(text);
#endif
我试过了,得到了:错误C2664:'std :: basic_string <_Elem,_Traits,_Ax> :: basic_string(const std :: basic_string <_Elem,_Traits,_Ax>&)':无法将参数1从'TCHAR [ 50]'const const std :: basic_string <_Elem,_Traits,_Ax>&' – 2013-01-04 01:23:06
@ user396483:我刚刚在VS2012中试过了。代码:[link](http://store.naszta.hu/main/example01.cpp)。 – Naszta 2013-01-04 21:34:22
TCHAR或者是char或wchar_t的,所以
typedef basic_string<TCHAR> tstring;
是这样做的一种方式。
另一种是完全跳过char
,只是使用std::wstring
。
我的回答是晚了,我承认,但“阿洛克保存”和一些研究的答案,我已经找到了一个好办法! (注:我没有测试这个版本很多,所以我测试了它应该它可能不会在任何情况下工作,但是从):
TCHAR t = SomeFunctionReturningTCHAR();
std::string str;
#ifndef UNICODE
str = t;
#else
std::wstring wStr = t;
str = std::string(wStr.begin(), wStr.end());
#endif
std::cout << str << std::endl; //<-- should work!
它仅适用于7位ASCII码,但会打印其他字符的垃圾。 – palota 2016-02-11 10:35:38
- 1. 如何将'System :: String ^'转换为'TCHAR'?
- 2. 如何将“指向常量TCHAR的指针”转换为“std :: string”?
- 3. 如何将std :: wstring转换为TCHAR *
- 4. 如何将System :: String ^转换为std :: string?
- 5. 将std :: string转换为char数组
- 6. 将TCHAr数组赋值(转换)为std :: string对象时发生构建错误
- 7. 将TCHAR数组转换为char数组
- 8. 将std :: __ cxx11 :: string转换为std :: string
- 9. 如何将unicode QString转换为std :: string?
- 10. 如何将char []转换为std :: string
- 11. 如何将wchar_t *转换为std :: string? ?
- 12. 如何将std :: string转换为LPCSTR?
- 13. 如何将Glib :: ustring转换为std :: string?
- 14. 将NSString转换为std :: string
- 15. 将std :: string转换为QString
- 16. 将std :: string转换为QwtText
- 17. 将uint64_t转换为std :: string
- 18. 将void *转换为std :: string
- 19. 将std :: string转换为char
- 20. 将std :: string转换为basic_ostream?
- 21. 如何将std :: string指针转换为std :: string
- 22. 如何将utf16 ushort数组转换为utf8 std :: string?
- 23. 将std :: string或string ^转换为C++/cli中的字节数组
- 24. C++/CX:将std :: string转换为Platform :: String^
- 25. 将std :: ostream转换为std :: string
- 26. 将std :: string转换为整数
- 27. 如何将Serializable转换为String数组?
- 28. 无法将参数2从'std :: string *'转换为'std :: string'
- 29. 如何将TCHAR转换为int
- 30. 将CStdStringW转换为TCHAR *
你意识到的std :: string仅仅是一个std一个typedef :: basic_string? –
2011-06-09 10:37:52
你是否想要将一个特定的Unicode或MBCS TCHAR(即真正的WCHAR或CHAR)总是转换为std :: string(即char),或者将CHAR转换为字符串并将WCHAR转换为wstring或其他东西? – Rup 2011-06-09 10:39:12