Home  >  Article  >  Operation and Maintenance  >  Garbled characters appear when running files written in Windows on Linux

Garbled characters appear when running files written in Windows on Linux

王林
王林Original
2019-12-03 11:43:303038browse

Garbled characters appear when running files written in Windows on Linux

Question:

Copy the code (.m) file written in matlab under Windows to Linux (Ubuntu), and the comments are in Chinese It's all gibberish, and vice versa, it's the same problem.

Reason:

The default encoding used under Windows is GB2312, and the default used under Linux is UTF-8. Therefore, the code generated under Windows is GB2312 encoding, which is of course recognized as garbled code under Linux; conversely, the code is garbled in the same way.

Free video tutorial recommendation: linux video tutorial

Solution:

Transcode the file encoding format directly.

Use the iconv command under linux to change the encoding of the file:

test1.m is converted from GB2312 to UTF-8 (the code of matlab under windows runs under linux)

iconv  -f  GB2312  -t  UTF-8  test1.m  -o  test1.m

test2.m is converted from UTF-8 to GB2312 (the code of matlab under Linux runs under windows)

iconv -f  UTF-8  -t  GB2312  test2.m  -o  test2.m

If there are too many files and batch processing is required, the shell written as follows can be used Script batch conversion encoding.

Usage: Place the script in the folder that needs to be converted, and execute the script to convert all files in the current directory and its subdirectories into the specified encoding method.

The execution is as follows:

Convert from GB2312 to UTF-8

./convertMatlab.sh  win

Convert from UTF-8 to GB2312

./convertMatlab.sh  linux

Tips: Ordinary users do not have execution permissions , you need to use chmod x convertMatlab.sh to increase execution permissions

Recommended related articles and tutorials: linux tutorial

The above is the detailed content of Garbled characters appear when running files written in Windows on Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn