views:

881

answers:

4

Hi, 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

+6  A: 

Some solutions:

  • Write to std::wcout instead
  • Convert:
    • The standard way, using std::codecvt
    • The Win32 way, using WideCharToMultibyte
Éric Malenfant
+3  A: 

For printing to cout, you should use std::wcout instead.

As for the comparison, I'm not really sure what you mean.

  • if 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.
  • and the 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.
jalf
I assume you meant `wcscmp`? C++ really should have overloaded `strcmp()` to take anything stringlike. But unfortunately only the math part of the standard C library got that treatment.
MSalters
True. I meant "the strcmp family of functions". That's why I said "a function like".
jalf
+1  A: 

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.

Paul Baker
+1  A: 

use the following

#ifdef UNICODE
#define tcout wcout
#else
#define tcout cout
#endif
Asad Ullah Khan