Terminal in vscode is garbled

Asked 2 years ago, Updated 2 years ago, 180 views

Windows 10pro
vscode version 1.25.1

Japanese is garbled at the terminal of vscode.I set "files.autoGuessEncoding":true in the user settings, but it doesn't fix.What should I do?
I'm a beginner in programming, so I'm sorry if I said something strange.

"files.encoding": "shiftjis" was added, but both the editor and terminal were garbled.

Screenshot of garbled characters

"terminal.integrated.shellArgs.windows": No characters were not printed when adding ["-NoExit", "chcp65001"] to the configuration.

Screenshot of how output disappears when you change settings

There are programs that work properly and programs that don't work.
Example of a moving program

"You can change the ""\n"" in the third image to ""endl""."
Enter a description of the image here

Also, when I try to write a new program just a while ago, a display like iostream file not found appears.It is still compiled and executed correctly.English characters are printed, but Japanese is not printed.
Enter a description of the image here

Enter a description of the image here

Enter a description of the image here

garbled-characters vscode

2022-09-30 11:46

3 Answers

The "files.autoGuessEncoding" · "files.encoding" setting determines the encoding of the file.
You do not configure terminal encoding.

  • Solution 1
    Change the terminal Powershell character code.
    To change the character code, use the chcp command.
    Run chcp65001 to change to UTF-8.
    Run chcp932 to change to sjis.
    Check the Microsoft document for the assigned number of each character code.
    chcpTo automatically run at startup without manually entering the command
    "terminal.integrated.shellArgs.windows":["-NoExit", "chcp65001"] in the configuration file.

  • Solution 2
    Powershell can be loaded without changing the settings if it is a SJIS, UTF-8BOM, UTF-16.
    Change the encoding of the file to load.

Solution 1
Change the terminal Powershell character code.
To change the character code, use the chcp command.
Run chcp65001 to change to UTF-8.
Run chcp932 to change to sjis.
Check the Microsoft document for the assigned number of each character code.
chcpTo automatically run at startup without manually entering the command
"terminal.integrated.shellArgs.windows":[-NoExit", "chcp65001"] in the configuration file.

Solution 2
Powershell can be loaded without changing the settings if it is a SJIS, UTF-8BOM, UTF-16.
Change the encoding of the file you want to import.

Enter a description of the image here
Click on the character code part of the bar below to save it with encoding→ Select the character code you want to save


2022-09-30 11:46

Let's assume you're using MinGW64GCC.Visual Studio (Visual C++) is different.

To avoid confusion, remove all "files.encoding", "terminal.integrated.shellArgs.windows", "files.autoGuessEncoding", and restart Visual Studio Code.

Source code should be UTF-8 (If you also want to compile Visual C++ or want to see source code on PowerShell, use UTF-8 with BOM).The rest works by adding the -fexec-charset=CP932 option when compiling (both gcc and g++).

g++-fexec-charset=CP932 hello.cpp

Below is the explanation.

There is no standard character code in C/C++.Therefore, it may not work well if you do not specify the correct character code during compilation.The complicated thing is that the three character codes are used separately.They are as follows:

The compiler automatically converts the character code from 1. to 2. or 3.What these character codes are depends on your environment and compiler.

For MinGW64GCC

For Visual C++

Another thing to note is the behavior of printf and iostream.They deal with strings (char*), but they do not perform character code conversion through the locale and print the data as it is.Therefore, if the character code of the string itself does not match the character code of the output destination, garbled characters will occur.(Wide string output may be character-coded to match the locale)

PowerShell in Visual Studio Code integrated terminal also has a character code, defaulting to locale dependency (CP932 in Japanese).In other words, the cause of garbled characters is that the character code in the string literal on the compiled binary in 2. does not match the character code in the terminal.

The GCC allows you to specify these three character codes at compile time.-fexec-charset=CP932 is the character code for the Japanese PoweShell.

[Supplement]

  • CP932 is a subspecies of Shfit_JIS used in Windows.It is sometimes referred to as MS932 or Windows-31J.
  • An alternative is to make the PowerShell side UTF-8, where chcp65001 is the command.
  • Sometimes the source code can be compiled with Shift_JIS, but it's just a broken UTF-8.In particular, compilation errors may occur if the second byte is \, such as 'Table'.
  • The
  • u8"..." literal added from C11/C++11 will be UTF-8 literal regardless of the above specification.Useful when you want UTF-8 to force string literals independent of your environment.
  • char*Note that there is no character code information in itself.


2022-09-30 11:46

Try changing the default character encoding from utf-8 to the character encoding that your file uses.

For example, if you want the default to Shift-JIS, add the following line after the line "files.autoGuessEncoding": true:

"files.encoding": "shiftjis",


2022-09-30 11:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.