Conversion of file encoding formats in Linux

Source: Internet
Author: User

Conversion of file encoding formats in Linux

Recently I put the project on github, but I found some Chinese characters in the Code are garbled. After checking, I found that my Centos was installed on a virtual machine, while I write code through UE in Windows, and UE uses ASCII encoding by default.

Therefore, you want to use commands in Linux to convert the encoding format in batches.

 

After checking the information, you can useIconvCommand.

 

UseFileCommand to detect the file type

For example:Filetest. cpp

Output:ISO-8859 Cprogram text

 

Parameters of the iconv command are described as follows:

 

-L list all known character sets

-F Original Text Encoding

-T output text encoding

-O output file name

-S close warning

 

Example:

Iconv-f GB2312-t UTF-8 test. cpp> test_utf.cpp

Because iconv outputs standard output by default, We need to redirect to another file. (The object content cannot be redirected to itself. Otherwise, the object content will be cleared)

 

If you want to directly output the output content to the current file, you can use the following method:

Iconv-f GB2312-t UTF-8-o test. cpp test. cpp


How to view the file encoding format and convert the file encoding in linux

For example, the C \ C ++ program written in Visual Studio needs to be compiled on the Linux host, while the Chinese comments of the program are garbled, which is serious due to encoding, the compiler on linux reports an error.
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

In Linux, how does one view the file encoding format? Run vim and set fileencoding = UTF-8.

File + file name

For more information, see blog.csdn.net/..991425.
View File Encoding
You can view the file encoding in Linux in the following ways:
1. 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
File encoding and conversion
1. Convert the file encoding directly in Vim. For example, convert a file to UTF-8 format.
: Sets fileencoding = UTF-8
2. enconv conversion file encoding, for example, to convert a GBK encoded file into UTF-8 encoding, the operation is as follows
Enconv-L zh_CN-x UTF-8 filename
3. iconv conversion. The iconv command format is as follows:
Iconv-f encoding-t encoding inputfile
For example, converting a UTF-8-encoded file into GBK Encoding
Iconv-f GBK-t UTF-8 file1-o file2
 

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.