M
M
Mars362021-06-18 13:57:08
C++ / C#
Mars36, 2021-06-18 13:57:08

How to use unicode in windows console with wstring?

Tried the most popular options.
At the moment, progress has stopped at this point:

system( "chcp 65001" );
    _setmode( _fileno( stdout ), _O_U8TEXT );
    _setmode( _fileno( stdin ), _O_U8TEXT );
    std::wcout << L"строка" << std::endl;
    std::wcout << L"string" << std::endl;
    std::wstring test;
    std::wcout << L"input: ";
    std::wcin >> test;
    std::wcout << test << std::endl;

The resulting output:
Active code page: 65001
строка
string
input: строка

Press <RETURN> to close this window...

Those. the first two options work fine, but in the case of input to the console, unknown characters are obtained.
60cc7bfa737da157285127.png
How can this be fixed?
So how can you solve the problem that the program accepts command line arguments, which by default is 866 and the characters will come in this encoding anyway and changing the encoding at runtime will not solve the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2021-06-18
@Mars36

The command line arguments will not necessarily be in 866 encoding, they can be in 1251 as well - it depends on the console encoding, which can be changed with the chcp console command.
In general, you should not use Unicode in the console, because the Windows console is not too friendly with Unicode.
For example, if you want to process the output of your program with some kind of filter in a batch file, then there may be problems with Unicode.
Although you can use unicode inside a program, in this case you will have to recode all input and output. Not sure if the standard library can be configured to do the transcoding itself.
And so in WinAPI there is a whole set of console functions, incl. you can get the current console encoding for input and output, set the desired encoding, recode the text.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question