[英]How to convert a TCHAR array to std::string?
How do I convert a TCHAR
array to std::string
(not to std::basic_string
)?如何将TCHAR
数组转换为std::string
(而不是std::basic_string
)?
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
).标准模板库支持 ASCII(使用std::string
)和宽字符集(使用std::wstring
)。 All you need to do is to typedef String as either std::string or std::wstring depending on your compilation configuration.您需要做的就是根据您的编译配置将String类型定义为 std::string 或 std::wstring 。 To maintain flexibility you can use the following code:为了保持灵活性,您可以使用以下代码:
#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
并让编译器处理讨厌的部分。 String will now have constructors that lets you convert TCHAR
to std::string
or std::wstring
. String 现在将具有构造函数,可让您将TCHAR
转换为std::string
或std::wstring
。
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!
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.所以如果你必须使用std::string
而不是std::wstring
,你应该使用转换器 function。 I may use wcstombs
or WideCharToMultiByte
.我可以使用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
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
。
Simple!简单的!
std::string tcharToChar(TCHAR* buffer)
{
char *charBuffer = NULL;
std::string returnValue;
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] = '\0';
strcat(charBuffer, singleCharacter);
free(singleCharacter );
}
strcat(charBuffer, "\0");
returnValue.append(charBuffer);
free(charBuffer);
return returnValue;
}
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.