Question

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?

Was it helpful?

Solution

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;

OTHER TIPS

Can I suggest std::wcout ?

So, something like this:

std::cout << "ASCII and ANSI" << std::endl;
std::wcout << L"INSERT MULTIBYTE WCHAR* HERE" << std::endl;

You might find more information in a related question here.

You could use use a normal char array that is actually filled with utf-8 characters. This should allow mixing characters across languages.

#include <iostream>
using namespace std;
void main()
{
setlocale(LC_ALL, "Russian");
cout << "\tДОБРО ПОЖАЛОВАТЬ В КИНО!\n";
}

Windows has the very confusing information. You should learn C/C++ concept from Unix/Linux before programming in Windows.

wchar_t stores character in UTF-16 which is a fixed 16-bit memory size called wide character but wprintf() or wcout() will never print non-english wide characters correctly because no console will output in UTF-16. Windows will output in current locale while unix/linux will output in UTF-8, all are multi-byte. So you have to convert wide characters to multi-byte before printing. The unix command wcstombs() doesn't work on Windows, use WideCharToMultiByte() instead.

First you need to convert file to UTF-8 using notepad or other editor. Then install font in command prompt console so that it can read/write in your language and change code page in console to UTF-8 to display correctly by typing in the command prompt "chcp 65001" while cygwin is already default to UTF-8. Here is what I did in Thai.

#include <windows.h>
#include <stdio.h>

int main()
{
    wchar_t* in=L"ทดสอบ"; // thai language
    char* out=(char *)malloc(15);
    WideCharToMultiByte(874, 0, in, 15, out, 15, NULL, NULL);
    printf(out); // result is correctly in Thai although not neat
}

Note that 874=(Thai) code page in the operating system, 15=size of string

My suggestion is to avoid printing non-english wide characters to console unless necessary because it is not easy.

You can print wide characters with wprintf.

#include <iostream>

int main()
{
    wchar_t en[] = L"Hello";
    wchar_t ru[] = L"Привет"; //Russian language
    wprintf(en);
    wprintf(ru);
    return 0;
}

Output:

Hello
Привет

we are not looking for easy ways!

int i = 0;
while (ru[i]) {
   std::cout << (char)ru[i];
   i++;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top