Reputation: 8614
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
Upvotes: 1
Views: 9912
Reputation: 31
use the following
#ifdef UNICODE
#define tcout wcout
#else
#define tcout cout
#endif
Upvotes: 3
Reputation: 248039
For printing to cout
, you should use std::wcout
instead.
As for the comparison, I'm not really sure what you mean.
var
is a wchar_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 like strcmp
which compares C-style strings (char pointers), or to use the C++ string class.operator==
usually returns a bool
, not an integer. So it can return false
, but it can't return 0
... Unless you've created some weird overload yourself. (and that is only possible if var
is a user-defined type.Upvotes: 9
Reputation: 14348
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 as wcscmp
.
Upvotes: 4
Reputation: 14148
Some solutions:
Upvotes: 12