2

I've been trying to print Chinese Characters in C++. I've already searched around in the Internet, some said that you have to use wcout, others have suggested other methods. I've also stumbled on this post, where someone uses a piece of code:

#include <iostream>
int main()
{
char x[] = "中";
char y[] = u8"中";
wchar_t z = L'中';
char16_t b = u'\u4e2d';
char32_t a = U'\U00004e2d';

std::cout << x << '\n';
std::cout << y << '\n';
std::wcout << z << '\n';
std::cout << a << '\n';
std::cout << b << '\n';    
}

which, on an internet site that shows the output of C++ code, prints:

中
中
-
20013
20013

However, for me it just prints

õ©¡
õ©¡
20013
20013

I'm using JetBrains CLion, with encoding set to UTF-8. However, I've also tried Visual Studio and QT Creator, I get the same result. I hope someone can help me out.

El3ctroGh0st
  • 61
  • 2
  • 5
  • 3
    The problem comes from the terminal (where cout writes to) not using all possible symbols, but just a subset of the whole Unicode. – Ripi2 Sep 12 '17 at 18:40
  • 2
    Possible duplicate of [How to print Unicode character in C++?](https://stackoverflow.com/questions/12015571/how-to-print-unicode-character-in-c) – Ripi2 Sep 12 '17 at 18:42

1 Answers1

0

If you're using OSX Terminal maybe you can check the Encoding.

Terminal -> Preferences -> Encodings Tab

Then check if Traditional Chinese is checked or Unicode (UTF-8).

For Windows, you can try this, to change to UTF-8 encoding.

Go to Start then Run "regedit" -> Navigate to [HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\Autorun] -> modify value to "chcp 65001"

Hope this helps.