When operating files in Windows in Linux, garbled characters are often encountered. For example, C \ c ++ written in Visual StudioProgramIt needs to be compiled on the Linux host, and the Chinese comments of the program are garbled. What is more serious is that the compiler on Linux reports an error due to encoding.
This is because the default file format in Windows is GBK (gb2312), while Linux is generally a UTF-8. In Linux, how does one view the file encoding and convert the file encoding?
Use Vim to view file encoding
You can directly view the file encoding in vim.
: Set fileencoding
The file encoding format is displayed.
If you only want to view files in other encoding formats or want to solve the problem of using Vim to View File garbled characters, you can
~ /Add the following content to the vimrc file:
Set encoding = UTF-8 fileencodings = ucs-bom, UTF-8, cp936
In this way, VIM can automatically identify the file encoding (can automatically identify the UTF-8 or GBK encoding files), in fact, according to the fileencodings provided by the encoding list to try, if not find the appropriate encoding, it is opened in Latin-1 (ASCII) encoding.
UseIconvFile encoding and conversion
We use the iconv tool to convert the file encoding.
Iconv conversion. The iconv command format is as follows:
Iconv-F encoding-T encoding inputfile
For example, converting a GBK-encoded file into a UTF-8 code
Iconv-f gbk-T UTF-8 file1-O file2
Parameter meaning
-F from an Encoding
-T to an Encoding
-O output to file