Suppose we have a document code.cpp , which was saved in windows-1251 encoding:

 //.......header char z = 172 std::cout<< z << std::endl; // выведет ¬ //....... 

Deduced what we expected, drag. The encoding is such and the symbol corresponds to it, but on the other hand, if we are on Windows, the console will work with the OEM (Кодировка 866) encoding OEM (Кодировка 866) , and in this encoding the number 172 denotes a completely different character, namely м .

How is that? If the console reproduces the result, then should it not display it in its encoding?

  • And if you think? - Qwertiy
  • @Qwertiy Nothing comes to mind, which is why I ask. - D Thr.
  • 2
    The console does not receive the character м , but byte with code 172. It does not recode it itself, since it does not know anything about the encoding in which this byte is interpreted by the program. The conversion task lies on the runtime library (which is most likely lazy). - VladD
  • one
    @DmitryTrapov: Look here: ru.stackoverflow.com/q/459154/10105 - VladD
  • @VladD here, what you need. - D Thr.

3 answers 3

There is a number 172 . The symbol with codes 172 is displayed in the console. No matter what the encoding is now. The code is the same, and the symbol is different. Everything is logical.

  • The fact of the matter is that the character is the same, although the encoding of the console is different. That was the question. - D Thr.
  • @DmitryTrapov, did not understand. So what is displayed? - Qwertiy
  • The console is the OEM 866 encoding OEM 866 , in which the code 172 means another character. A character is displayed in the console for encoding win1251 . - D Thr.
  • @DmitryTrapov, and this is strange. It never worked for me. And which compiler? - Qwertiy
  • MinGW, which came with Code :: Blocks. - D Thr.

Use SetConsoleOutputCP () and output everything you need. But there is a nuance: in the target system there may not be fonts with symbols of the necessary CodePage.

    Only now I realized the whole nonsense of the question. I just misled. The same characters were output exactly according to that, because my setlocale(LC_ALL, "Russian") was declared in the code. And so the console reproduces exactly OEM 866 кодировку .