76

I had the code:

std::string st = "SomeText";
...
std::cout << st;

and that worked fine. But now my team wants to move to wstring. So I tried:

std::wstring st = "SomeText";
...
std::cout << st;

but this gave me a compilation error:

Error 1 error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from 'const char [8]' to 'const std::basic_string<_Elem,_Traits,_Ax> &' D:...\TestModule1.cpp 28 1 TestModule1

After searching the web I read that I should define it as:

std::wstring st = L"SomeText"; // Notice the "L"
...
std::cout << st;

this compiled but prints "0000000000012342" instead of "SomeText".

What am I doing wrong ?

BartoszKP
  • 33,416
  • 13
  • 100
  • 127
Roee Gavirel
  • 18,150
  • 12
  • 61
  • 88
  • Consider using ICU unicode library - [link](http://icu-project.org/apiref/icu4c/index.html). It supports unicode and has many usefull features. especially if you want to manipulate those strings. – WeaselFox Jan 09 '12 at 12:06
  • @Weasel: Why in the world would you need a separate library to work with Unicode strings? – Cody Gray Jan 09 '12 at 12:08
  • from my experience its much better to use a separate library to manipulate wchar strings (search, replace etc) or other multi-byte strings. – WeaselFox Jan 09 '12 at 12:31
  • Thanks, but I just needed to use wcout instead of cout (-: – Roee Gavirel Jan 09 '12 at 12:43

6 Answers6

136

To display a wstring you also need a wide version of cout - wcout.

std::wstring st = L"SomeText";
...
std::wcout << st; 
Bo Persson
  • 88,437
  • 31
  • 141
  • 199
20

Use std::wcout instead of std::cout.

hmjd
  • 117,013
  • 19
  • 199
  • 247
9

This answer apply to "C++/CLI" tag, and related Windows C++ console.

If you got multi-bytes characters in std::wstring, two more things need to be done to make it work:

  1. Include headers
    #include <io.h>
    #include <fcntl.h>
  2. Set stdout mode
    _setmode(_fileno(stdout), _O_U16TEXT)

Result: Multi-bytes console

Val
  • 20,060
  • 10
  • 64
  • 83
  • Thank you very much! It would have taken me a long time to figure this out... I was having trouble printing a wstring that I instantiated with a greater length than the data I was supplying due to `sizeof(wchar_t) == sizeof(char) * 2`, and then printing anything after that wasn't succeeding. Your answer fixed the multi-byte printing problem so I could determine what was going on. – Andrew Apr 14 '18 at 22:27
6

try to use use std::wcout<<st it will fix your problem.

std::wstring st = "SomeText";
...
std::wcout << st;
Hemant Metalia
  • 27,928
  • 18
  • 70
  • 89
1

Another way to print wide string:

std::wstring str1 = L"SomeText";
std::wstring strr2(L"OtherText!");

printf("Wide String1- %ls \n", str1.c_str());
wprintf(L"Wide String2- %s \n", str2.c_str());
  • For printf: %s is narrow char string and %ls is wide char string.
  • For wprintf: %hs is narrow char string and %s is wide char string.
Nikita Jain
  • 488
  • 5
  • 10
0

In addition to what is said above, there are octal and hexadecimal character encodings

std::wstring blankHex = L"\x0020";
std::wstring blankOct= L"\040";

String and character literals (C++)

Sam Ginrich
  • 494
  • 5
  • 6