How to print UTF-8 characters on the c++ console?

Asked

Viewed 3,378 times

3

The console incorrectly displays accented characters. How to print them correctly (UTF-8)?

2 answers

3

You’re probably referring to the Windows console, or cmd.exe.

By default, the cmd.exe does not work with the encoding outgoing UTF-8, also known as the Code Page code 65001.

To change it, just type the command in the console:

chcp 65001

Alternatively you can start the console already with the encoding output adjusted to UTF-8, let’s see:

1) Press Windows + R;

2) Execute the following command:

cmd.exe /K chcp 65001 

Follows a program in C++ able to test if the console has been configured correctly:

#include <iostream>
#include <locale>


int main(){

    setlocale( LC_ALL, "" );

    std::wstring ch = L"你好世界";
    std::wstring gk = L"γειά σου κόσμος";
    std::wstring jp = L"こんにちは世界";
    std::wstring ko = L"여보세요 세계";
    std::wstring pt = L"Olá mundo!";
    std::wstring ru = L"Здравствулте мир!";

    std::wcout << L"Chinês    : " << ch << std::endl;
    std::wcout << L"Grego     : " << gk << std::endl;
    std::wcout << L"Japonês   : " << jp << std::endl;
    std::wcout << L"Coreano   : " << ko << std::endl;
    std::wcout << L"Português : " << pt << std::endl;
    std::wcout << L"Russo     : " << ru << std::endl;

}

Exit:

Chinês    : 你好世界
Grego     : γειά σου κόσμος
Japonês   : こんにちは世界
Coreano   : 여보세요 세계
Português : Olá mundo!
Russo     : Здравствулте мир!

References:

chcp: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/chcp

  • Thank you. Would you know how to automate this on the Visual Studio 2017 console? That is, at the time of opening the console by VS, would already have this configuration?

0


1st declare the library: #include <locale.h>
2º place at the beginning of the main function:
setlocale(LC_ALL,""); // Este comando pega a codificação da maquina que está rodando.

#include <iostream>
#include <locale.h>    
using namespance std;
int main(){

setlocale(LC_ALL,"");

cout <<"Acento é legal =D "<< endl;

}       
  • 2

    A C++ answer would be better, due to the [tag:c++] of the question.

  • 1

    Actually it was the simplest and most efficient answer, solved the problem with only 2 lines of code.

  • 2

    This code adjusts your program to the console’s "encoding", and there is no guarantee that you will be working with a console on UTF-8 (as mentioned in the question). Ah! "Language" is not "encoding"!

  • 1

    @Rogériodec, I disagree, this answer does not solve your problem of being able to use UTF-8.

  • 1

    @Mario Feroldi, For me decided, because my question is: "How to print UTF-8 characters on the c++?console". And with these two lines, accented characters appear on the console. Why complicate?

  • 2

    @Rogériodec, I’m sorry for sounding rude. By this answer, the console is not using UTF-8 as encoding, but the current Windows, changing only the language. It is a technical difference, but very important and may end up generating confusion in the future.

  • @Márioferoldi I’m happy to have helped you. To the others, first of all thanks for the criticism, until then I didn’t know the difference between: character encoding and character set, as you have already noticed my knowledge is basic, but I like to share the little I know. Hug to all.

  • 1

    @Mario Feroldi, I need to give my arm to twist. I had to ask a question here to understand why a simple stof("1.9") brought only 1 and not 1.9. Then I discovered that setlocale(LC_ALL,"") changes even the numbering system to the Brazilian standard, that is, a semicolon. More confusion because of this command...

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.