Recently changed to VS2017, because at hand to do an MFC program, so write the console program to do functional testing, and then found incredibly garbled.
Then use VS2017 new Windows Console application, in the main function to add a sentence printf ("Hello"), the running result is still garbled
After opening the file with notapad++, click on the menu bar encoding an entry, found that is UTF-8 no BOM format encoding, and then changed to ANSI format after encoding
That is, VS is encoded with UTF-8, and the strings in the compiled program are encoded according to UTF-8, while the console is understood by ANSI coding.
For example, a with 0x00 means "yes", B with 0x00 for "No", A sends 0x00 to B, then B shows "no".
So let's start with the solution, which is to have the VS file ANSI-encoded on the line, and after searching the web, it's easy to find a solution.
VS menu item Tools--Customize, open the Customize dialog box, then select the Commands tab, click the Add command button
Then in [file] type found [advanced save options], click OK, you can find the VS menu bar more advanced Save Options column, as long as the cursor in the code file, you can click the menu item.
Change the code to Simplified Chinese (GB2312), then recompile and run, the display is correct.
The cmd window type input chcp command also lets you see that the active code page of the Windows command-line window is 936.
Of course, I import the modified file Project2.cpp and the modified file Project1.cpp into the Linux system, with the locale command can see the system by default is UTF-8 encoded
$ Localelang=en_us. utf-8LANGUAGE=En_uslc_ctype="en_US. UTF-8"lc_numeric="en_US. UTF-8"Lc_time="en_US. UTF-8"lc_collate="en_US. UTF-8"lc_monetary="en_US. UTF-8"lc_messages="en_US. UTF-8"Lc_paper="en_US. UTF-8"Lc_name="en_US. UTF-8"lc_address="en_US. UTF-8"Lc_telephone="en_US. UTF-8"lc_measurement="en_US. UTF-8"lc_identification="en_US. UTF-8"Lc_all=
Use the file command to see
file Project1. CPP Project1. cpp: C source, iso-8859file Project2. CPP Project2. cpp: C source, utf-8 Unicode text, with CRLF line terminators
One is ISO-8859, one is UTF-8, now UTF-8 is the trend, like the MFC string class CString as early as ten years ago vs is cstringw (that is, the MFC project type default encoding is UTF-8), just before the VS version of the species, The encoding of the console program is also adapted to the command-line window.
Also note that the second half sentence, line Terminators (newline character) is CRLF. Go back to just the Advanced Save Options dialog box
The line break in Windows is "\ r \ n", while the line break of the UNIX system (including Linux) is "\ n", and VS2017 in order to support other systems, encoding can be set for compatibility in advanced save options.
VS2017 new Windows Console program printing Chinese garbled problem