C++ 如何将 TCHAR 数组转换为 std::string?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6291458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a TCHAR array to std::string?
提问by ashmish2
How do I convert a TCHAR
array to std::string
(not to std::basic_string
)?
如何将TCHAR
数组转换为std::string
(而不是std::basic_string
)?
回答by Alok Save
TCHAR
is just a typedef that, depending on your compilation configuration, either defaults to char
or wchar_t
.
TCHAR
只是一个 typedef,根据您的编译配置,默认为char
或wchar_t
.
Standard Template Library supports both ASCII (with std::string
) and wide character sets (with std::wstring
). All you need to do is to typedefString as either std::string or std::wstring depending on your compilation configuration. To maintain flexibility you can use the following code:
标准模板库支持 ASCII(带std::string
)和宽字符集(带std::wstring
)。所有你需要做的是对的typedefString作为任何的std :: string或std ::根据您的编译配置wstring的。为了保持灵活性,您可以使用以下代码:
#ifndef UNICODE
typedef std::string String;
#else
typedef std::wstring String;
#endif
Now you may use String
in your code and let the compiler handle the nasty parts. String will now have constructors that lets you convert TCHAR
to std::string
or std::wstring
.
现在您可以String
在您的代码中使用并让编译器处理讨厌的部分。String 现在将具有构造函数,可让您转换TCHAR
为std::string
或std::wstring
。
回答by Naszta
TCHAR type is char
or wchar_t
, depending on your project settings.
TCHAR 类型为char
或wchar_t
,具体取决于您的项目设置。
#ifdef UNICODE
// TCHAR type is wchar_t
#else
// TCHAR type is char
#endif
So if you must use std::string
instead of std::wstring
, you should use a converter function. I may use wcstombs
or WideCharToMultiByte
.
因此,如果必须使用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
回答by Bo Persson
TCHAR is either char or wchar_t, so a
TCHAR 要么是 char 要么是 wchar_t,所以
typedef basic_string<TCHAR> tstring;
is one way of doing it.
是一种方法。
The other is to skip char
altogether and just use std::wstring
.
另一种是char
完全跳过并使用std::wstring
.
回答by Aemmel
My answer is late, I'll admit that, but with the answers of 'Alok Save' and some research I've found a good way! (Note: I didn't test this version a lot, so it might not work in every case, but from what I tested it should):
我的回答晚了,我承认这一点,但是通过“Alok Save”的答案和一些研究,我找到了一个好方法!(注意:我没有经常测试这个版本,所以它可能不适用于所有情况,但从我测试的情况来看应该是):
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!
回答by serup
Quick and dirty solution :
快速而肮脏的解决方案:
TCHAR str[256] = {};
// put something in str...
// convert to string
std::string strtmp(&str[0], &str[255]);
std::cout << strtmp << std::endl;
回答by user1567759
Simple!
简单的!
char* tcharToChar(TCHAR* buffer)
{
char *charBuffer = NULL;
int lengthOfbuffer = lstrlenW(buffer);
if(buffer!=NULL)
{
charBuffer = (char*)calloc(lengthOfbuffer+1,sizeof(char));
}
else
{
return NULL;
}
for (int index = 0;
index < lengthOfbuffer;
index++)
{
char *singleCharacter = (char*)calloc(2,sizeof(char));
singleCharacter[0] = (char)buffer[index];
singleCharacter[1] = '##代码##';
strcat(charBuffer, singleCharacter);
}
strcat(charBuffer, "##代码##");
return charBuffer;
}
Then capture the char * in std::string
然后在 std::string 中捕获 char *