8

I am having trouble with printing out korean. I have tried various methods with no avail. I have tried

1.

    cout << "한글" << endl;

2.

    wcout << "한글" << endl;

3.

    wprintf(L"한글\n");

4.

    setlocale(LC_ALL, "korean");
    wprintf("한글");

and more. But all of those prints "한글". I am using MinGW compiler, and my OS is windows 7.

P.S Strangely Java prints out Korean fine,

    String kor = "한글";
    System.out.println(kor);

works.

Abhipso Ghosh
  • 457
  • 1
  • 6
  • 21
  • 2
    The console is likely not using the right [code page](http://msdn.microsoft.com/en-us/library/windows/desktop/ms686013(v=vs.85).aspx). [FWIW](http://coliru.stacked-crooked.com/a/ff5de41c33edc7f1)... – chris Jul 26 '14 at 05:40
  • I thing "setlocale" changes code page as well as string format to korean with "korean" (or "ko_KR") argument. Is that right way to do it? – user3879095 Jul 26 '14 at 06:22
  • Also I've noticed that you used clang++ compiler. Maybe different it is MingGW compiler problem? – user3879095 Jul 26 '14 at 06:24
  • It is due to encoding – WannaBeCoder Jul 26 '14 at 07:12
  • Did you figure this out? I'm very curious since I've also sought to print complex scripts to the console. Unless I alter my system locale or use a wrapper program such as ConEmu that allows me to specify the font as well as the encoding, I cannot see such characters, even with Java or C#, the former of which apparently works for you. :-( –  Jul 27 '14 at 00:05
  • wow.. it was Windows problem. Qt now prints korean fine. So What I did was in the regions and langauge setting of windows, I changed the system locale to "Korea" and it works like a charm.. Thank you all for your helps!. – user3879095 Jul 27 '14 at 05:32

3 Answers3

2

Set the console codepage to utf-8 before printing the text

::SetConsoleOutputCP(65001)
dvasanth
  • 1,337
  • 1
  • 9
  • 10
0

Since you are using Windows 7 you can use WriteConsoleW which is part of the windows API. #include <windows.h> and try the following code:

DWORD numCharsToWrite = str.length();
LPDWORD numCharsWritten = NULL;
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str.c_str(), numCharsToWrite, numCharsWritten, NULL);

where str is the is a std::wstring

More on WriteConsoleW: https://msdn.microsoft.com/en-us/library/windows/desktop/ms687401%28v=vs.85%29.aspx

After having tried other methods this worked for me.

0

Problem is that there are a lot of places where this could be broken.

Here is answer I've posted some time ago (covers Korean). Answear is for MSVC, but same applies to MinGW (compiler switches are different, locale name may be different).

Here are 5 traps which makes this hard:

  • Source code encoding. Source has to use encoding which supports all required characters. Nowadays UTF-8 is recommended. It is best to make sure your editor (IDE) is properly configure to enforce source encoding.
  • You have to inform compiler what is encoding of source file. For gcc it is: -finput-charset=utf-8 (it is default)
  • Encoding used by executable. You have to define what kind of encoding string literals should be encode in final executable. This encoding should also cover required characters. Here UTF-8 is also the best. Gcc option is -fexec-charset=utf-8
  • When you run application you have to inform standard library what kind of encoding your string literals are define in or what encoding in program logic is used. So somewhere in your code at beginning of execution you need something like this (here UTF-8 is enforced):
std::locale::global(std::locale{".utf-8"});
  • and finally you have to instruct stream what kind of encoding it should use. So for std::cout and std::cin you should set locale which is default for the system:
    auto streamLocale = std::locale{""}; 
    // this impacts date/time/floating point formats, so you may want tweak it just to use sepecyfic encoding and use C-loclae for formating
    std::cout.imbue(streamLocale);
    std::cin.imbue(streamLocale);

After this everything should work as desired without code which explicitly does conversions.
Since there are 5 places to make mistake, this is reason people have trouble with it and internet is full of "hack" solutions.

Note that if system is not configured for support all needed characters (for example wrong code page is set) then with thsi configuration characters which could not be converted will be replaced with question mark.

Marek R
  • 32,568
  • 6
  • 55
  • 140