Solve the problem of Chinese garbled characters in the windows console (also applicable to VSCode and Idea terminals)

1.Problem description

I logged the output when running the electron project, but garbled characters were displayed on the terminal, as shown in the figure:
Insert image description here

2. Cause of the problem

This is because the Windows console uses GBK encoding by default, and our projects generally use UTF-8 encoding, so the log output to the console will produce garbled characters. We can just modify the encoding of the console.

3.Problem solving

First of all, I output the log to facilitate checking the problem during the development process, so I adopt the simplest solution. If in a production environment, the current approach may not be applicable.

This method only solves the encoding of the currently opened console. It does not modify the global encoding format. Open other consoles. The encoding is still GBK.
This method only solves the encoding of the currently opened console. It does not modify the global encoding format. Open other consoles. Console, the encoding is still GBK.
This method only solves the encoding of the currently opened console. It does not modify the global encoding format. When opening other consoles, the encoding is still GBK.
Take Idea as an example:

3.1 Open Idea’s Terminal

The console input chcp
Insert image description here
936 corresponds to GBK encoding. We need to change it to UTF-8, which corresponds to 65001.

3.2 Inputchcp 65001

Insert image description here

3.3 Start the program and view the logs

Insert image description here

Guess you like

Origin blog.csdn.net/Zhang_YingJie/article/details/126525269