I need to translate a string from cp1251 to unicode . For this, I use MutliByteToWideChar(1251, ...) .

The strangeness is that if you do not:

 setlocate(LC_ALL, "rus"); 

The characters with the codes [128-255] are translated incorrectly. Yes, I know that setlocale () sets the locale to use ... But I don’t understand what is the connection with the conversion function, which clearly indicated that the input string is in cp1251 .

  • the characters are interpreted by the program not as it should without him, from where he knows Russian or, for example, Spanish characters are written there. - ishidex2
  • If you type Hi for example, then the characters will "become" identical to their code. - ishidex2
  • The first parameter of the MultiByteToWideChar () function is to accept the code page. I am transferring 1251 there . - user294535
  • Are these typed translations? Use wprintf, and in general, the locale in the console should be installed with commands and install the TrueType font, if the application is console. See here how to install the font, and different options for translating the console to Unicode mode. - NewView
  • Incorrectly translated - this means that after translating a character into Unicode, its code does not correspond to the normal one. What does the console have to do with it? In my program it does not exist, and it does not at all relate to the issue under consideration. To set the console code page, there are functions SetConsoleCP () and SetConsoleOutputCP () - user294535

1 answer 1

setlocale sets the current locale for the functions of the standard C library. It does not affect the MutliByteToWideChar function. This is easy to see in practice, this code works fine without the need to use setlocale :

 #include <stdio.h> #include <stdlib.h> #include <windows.h> int __cdecl main(int argc, char **argv) { char str[]="Привет"; wchar_t* wstr = new wchar_t[1024]; MultiByteToWideChar( 1251 , 0 , str , sizeof(str), wstr , 1024 ); HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); WriteConsoleW(consoleHandle, wstr, wcslen(wstr), NULL, NULL); getchar(); return 0; } 

Look for a problem elsewhere.