How to view file encoding formats and convert file encoding in Linux

Source: Internet
Author: User

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?

1. Use Vim to view the file encoding

You can directly view the file encoding in vim.

: Set fileencoding

The file encoding format is displayed.

2. Rewrite ~ /. Vimrc File

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.

3. iconv file 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

4. Save the file as a file in notepad to change the file encoding;

Use notepad in Windows to open the file and choose Save ......, Change the encoding.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.