C ++:LPWSTR打印为cout中的地址 [英] C++: LPWSTR prints as an address in cout
问题描述
我有一个变量类型 LPTSTR
,我打印到 std :: cout
与<<
。在ANSI系统(不知道它确定在哪里确定)它工作正常,它打印字符串。现在在Unicode系统中,我得到一个十六进制地址而不是字符串。那么,为什么 LPSTR
(如果 UNICODE >
与 LPTSTR
code>未定义)不同之处LPWSTR
(... if UNICODE $ c $
对于Unicode字符串您需要 wcout
。
您可能会看到十六进制,因为ANSI / ASCII输出流不知道如何处理Unicode字符。
LPTSTR
和 LPWSTR
继承自C Windows API天的C语言。对于C ++,我强烈建议您改用 std :: string
和/或 std :: wstring
p>
如果您需要滚动自己的宏,您将需要以下内容:
#ifdef _UNICODE
#define COUT wcout
#else
#define COUT cout
#endif
I have a variable of type LPTSTR
, which I print to std::cout
with <<
. In an ANSI system (don't know exactly where it is determined) it worked fine, it printed the string. Now in a Unicode system I get a hex address instead of the string. So, why does LPSTR
(to which LPTSTR
is resolved if UNICODE
is not defined) act differently from LPWSTR
(... if UNICODE
is defined) and how do I print the string pointed by the latter one?
For Unicode strings you want wcout
.
You may be seeing hex because the ANSI/ASCII output stream doesn't know how to handle Unicode characters.
LPTSTR
and LPWSTR
are actually C-isms inherited from the C Windows API days. For C++ I would strongly encourage you to use std::string
and/or std::wstring
instead.
If you need to roll your own macro, you'll want something like:
#ifdef _UNICODE
#define COUT wcout
#else
#define COUT cout
#endif
这篇关于C ++:LPWSTR打印为cout中的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!