3

For example:

cout << "你好" << endl;
user3234
  • 303
  • 2
  • 4
  • 8
  • 1
    @Falmarri: And if you try and it works on your machine, what would that tell about whether it works on your customers' machines?? – sbi Feb 02 '11 at 19:59

5 Answers5

3

It depends on the platform and on the encoding of the command prompt.

That said, you might have more luck with

std::wcout << L"你好" << std::endl;
sbi
  • 219,715
  • 46
  • 258
  • 445
  • depends on the compiler and relevant usage. visual c++'s runtime library does a lot of machinations, **detecting** whether the output goes to a console window or not, and adjusting the behavior. that tends to mislead programmers about what console windows do or don't do, and about what the wide streams do and don't do. – Cheers and hth. - Alf Apr 11 '13 at 21:41
2

Since you're talking Windows you're talking console windows.

Windows console windows are Unicode-based, more specifically UCS-2 (the Basic Multilingual Plane, essentially original 16-bit Unicode).

Contrary to what's suggested in other answers you can't really do more wrt. results with std::wcout than with std::cout. The standard wide output streams just translate to the program's single byte execution character encoding. Which by default in Windows is the OEM character encoding (which can be configured via an undocumented registry key).

But you can output Unicode via the Windows API's console functions.

Note: it's not a good idea to set console windows to UTF-8. Then the [cmd.exe] command interpreter just silently ignores most commands. At least in Windows XP and earlier.

Me I just set OEM to Windows ANSI Western (via the mentioned undocumented registry key) and it works for most purposes, but not Chinese of course. It's codepage 1252. Alternatively you can do that manually via command chcp 1252 in each command interpreter instance, and also other ways.

Cheers & hth.,

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
1

Depends on the operating system and implementation. The currently active C++ standard doesn't even talk about Unicode.

On Windows I believe you can use the wide character versions. Your code would then need to be:

std::wcout << L"ni3 hao3" << std::endl;

However, you might have to specifically enter the Unicode character codes because the codeset you're using to write your C++ source might not be Unicode.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
1

It all depends on the console/shell you are using.

Some shells can be configured to use UTF-8 (I did it once a long time ago) so it can be done but I forget the details.

Before anbody can really be more specific we need to know the shell you are using and under what platform the shell is running (with full details about versions of each).

Martin York
  • 257,169
  • 86
  • 333
  • 562
1

The Windows console supports Unicode, partially. It does not support, for example, right-to-left languages.

Your example with cout does not output unicode. Use wcout instead. Or use WriteConsoleW

John
  • 5,561
  • 1
  • 23
  • 39