Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can print the wchar_t values to console?

Example:

#include <iostream>  using namespace std;  int main() {     wchar_t en[] = L"Hello";     wchar_t ru[] = L"Привет"; //Russian language     cout << ru          << endl          << en;     return 0; } 

This code only prints HEX-values like adress. How to print the wchar_t string?

like image 245
zed91 Avatar asked Mar 22 '10 16:03

zed91


People also ask

How do I print a wide character in C++?

We can see that to make wide character we have to add 'L' before the character literal. But the character value is not displayed in the output using cout. So to use wide char we have to use wcout, and for taking input we have to use wcin. We can make some wide character array, and print them as string.

What encoding is Wchar_t?

The wchar_t type is an implementation-defined wide character type. In the Microsoft compiler, it represents a 16-bit wide character used to store Unicode encoded as UTF-16LE, the native character type on Windows operating systems.


1 Answers

Edit: This doesn’t work if you are trying to write text that cannot be represented in your default locale. :-(

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

wcout << ru << endl << en; 
like image 135
Nate Avatar answered Sep 21 '22 06:09

Nate