C++、WCHAR[] 到 std::cout 和比较
我需要将 WCHAR[] 放入 std::cout ...它是从 Native Wifi API 回调传递的 PWLAN_CONNECTION_NOTIFICATION_DATA 的一部分。
我尝试了简单的 std::cout <<变量;但它打印出第一个字符的数字地址。比较 (var == L"some text"
) 也不起作用。调试器返回预期值,但比较返回 0。如何将此数组转换为标准字符串(std::string)?
提前致谢
I need to put WCHAR[] to std::cout ... It is a part of PWLAN_CONNECTION_NOTIFICATION_DATA passed from Native Wifi API callback.
I tried simply std::cout << var; but it prints out the numeric address of first char. the comparision (var == L"some text"
) doesn't work either. The debugger returns the expected value, however the comparision returns 0. How can I convert this array to a standard string(std::string)?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一些解决方案:
Some solutions:
要打印到
cout
,您应该使用std::wcout
。至于比较,我不太明白你的意思。
var
是wchar_t[]
,那么您正在比较两个指针。结果很可能是假的,因为虽然字符串内容可能相同,但它们在物理上分配在不同的内存位置。答案是使用类似strcmp
的函数来比较 C 样式字符串(字符指针),或者使用 C++ 字符串类。operator==
通常返回一个bool
,而不是一个整数。因此它可以返回false
,但不能返回0
...除非您自己创建了一些奇怪的重载。 (只有当var
是用户定义的类型时才有可能。For printing to
cout
, you should usestd::wcout
instead.As for the comparison, I'm not really sure what you mean.
var
is awchar_t[]
, then you are comparing two pointers. And the result will most likely be false, because while the string contents may be the same, they are physically allocated in different memory locations. The answer is to either use a function likestrcmp
which compares C-style strings (char pointers), or to use the C++ string class.operator==
usually returns abool
, not an integer. So it can returnfalse
, but it can't return0
... Unless you've created some weird overload yourself. (and that is only possible ifvar
is a user-defined type.假设 var 是一个
wchar_t *
,var == L"some text"
进行指针比较。为了比较 var 指向的字符串,请使用诸如wcscmp
之类的函数。Assuming var is a
wchar_t *
,var == L"some text"
does a pointer comparison. In order to compare the string pointed to by var, use a function such aswcscmp
.使用以下内容
use the following