的std :: wstring.c_str()返回一个wchar_t的*。

我如何从wchar_t的*得到TCHAR *,或从的std :: wstring的,以TCHAR *

由于

有帮助吗?

解决方案

#include <atlconv.h>

TCHAR *dst = W2T(src.c_str());

会做正确的事ANSI或Unicode版本。

其他提示

使用这样的:

wstring str1(L"Hello world");
TCHAR * v1 = (wchar_t *)str1.c_str();

TCHAR *被定义为为wchar_t *如果UNICODE被定义,否则它是字符*。所以,你的代码可能是这个样子:

wchar_t* src;
TCHAR* result;
#ifdef UNICODE
result = src;
#else
//I think W2A is defined in atlbase.h, and it returns a stack-allocated var.
//If that's not OK, look at the documenation for wcstombs.
result = W2A(src);
#endif

常规这是不可能的,因为wchar_t的未必是大小相同TCHAR

几个解决方案已经列出的字符集之间的转换。如果字符集的范围被转换重叠这些可以工作。

我宁愿回避该问题完全尽可能并使用已在TCHAR字符集定义的标准字符串如下:

typedef std::basic_string<TCHAR> tstring;

使用这种

您现在有一个标准库兼容的字符串,这也是与Windows TCHAR宏兼容。

您可以使用:

wstring ws = L"Testing123";
string s(ws.begin(), ws.end());
// s.c_str() is what you're after

假设你在Windows操作系统。

如果您在Unicode生成配置,然后TCHARwchar_t是一样的。你可能会取决于你是否有/ Z为wchar_t设置是一类与wchar_t的是一个typedef需要铸造。

如果你在一个多字节构建配置,则需要MultiByteToWideChar(和正相反)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top