Output unicode strings in Windows console app

后端 未结 11 1277
花落未央
花落未央 2020-11-21 11:20

Hi I was trying to output unicode string to a console with iostreams and failed.

I found this: Using unicode font in c++ console app and this snippet work

11条回答
  •  佛祖请我去吃肉
    2020-11-21 11:52

    Correctly displaying Western European characters in the windows console

    Long story short:

    1. use chcp to find which codepage works for you. In my case it was chcp 28591 for Western Europe.
    2. optionally make it the default: REG ADD HKCU\Console /v CodePage /t REG_DWORD /d 28591

    History of the discovery

    I had a similar problem, with Java. It is just cosmetic, since it involves log lines sent to the console; but it is still annoying.

    The output from our Java application is supposed to be in UTF-8 and it displays correctly in eclipse's console. But in windows console, it just shows the ASCII box-drawing characters: Inicializaci├│n and art├¡culos instead of Inicialización and artículos.

    I stumbled upon a related question and mixed some of the answers to get to the solution that worked for me. The solution is changing the codepage used by the console and using a font that supports UNICODE (like consolas or lucida console). The font you can select in the system menu of the Windows cosole:

    1. Start a console by any one of
      • Win + R then type cmd and hit the Return key.
      • Hit the Win key and type cmd followed by the return key.
    2. Open the system menu by any one of
      • click the upper left corner icon
      • Hit the Alt + Space key combination
    3. then select "Default" to change the behavior of all subsequent console windows
    4. click the "Font" tab
    5. Select Consolas or Lucida console
    6. Click OK

    Regarding the codepage, for a one-off case, you can get it done with the command chcp and then you have to investigate which codepage is correct for your set of characters. Several answers suggested UTF-8 codepage, which is 65001, but that codepage didn't work for my Spanish characters.

    Another answer suggested a batch script to interactively selecting the codepage you wanted from a list. There I found the codepage for ISO-8859-1 I needed: 28591. So you could execute

    chcp 28591
    

    before each execution of your application. You might check which code page is right for you in the Code Page Identifiers MSDN page.

    Yet another answer indicated how to persist the selected codepage as the default for your windows console. It involves changing the registry, so consider yourself warned that you might brick your machine by using this solution.

    REG ADD HKCU\Console /v CodePage /t REG_DWORD /d 28591
    

    This creates the CodePage value with the 28591 data inside the HKCU\Console registry key. And that did work for me.

    Please note that HKCU ("HKEY_CURRENT_USER") is only for the current user. If you want to change it for all users in that computer, you'll need to use the regedit utility and find/create the corresponding Console key (probably you'll have to create a Console key inside HKEY_USERS\.DEFAULT)

提交回复
热议问题