Table of contents
TL;DR / [Geek Summary]:
- Encoding mismatch is the root of all evil; it’s a battle between ANSI and UTF-8.
- Brute Force: Simply Save As ANSI using Notepad—quick, dirty, and effective.
- Elegant Upgrade: Inject the
chcp 65001command at the script’s header to force the code page to UTF-8.
# question
The bat batch processing script is as follows
| |
When CMD executes the script, Chinese garbled characters appear.
# analyze
Garbled characters are all related to character encoding. .
Computers can only process numbers. If text is encountered, it must be converted to a number before processing. This is where character encodings (character sets) come in. . If you use a certain character encoding for encoding, you must use the same character encoding for decoding. If different character encodings are used, garbled characters will appear. .
# solve
# Convert the encoding of script files
By default, in the bat script file, if the Chinese is not ANSI encoding, garbled characters will appear. Therefore, you can use the Notepad that comes with the Windows operating system platform to open the script file, click the menu [File] - [Save As], and select ANSI encoding to save.
After converting the encoding and running it again, the Chinese garbled problem was solved.
# Change code
Declare changes to the previous code in the original bat script file
| |
# other
- Common code page mapping
| code page | mapped character set |
|---|---|
| 936 | GB2312 |
| 20127 | US-ASCII |
| 65001 | UTF-8 |