Windows go to Linux, file garbled, file encoding conversion

Source: Internet
Author: User

reproduced:http://www.cnblogs.com/wanyao/p/3399269.html

Recently, learning to start Linux learning again, so has been in CentOS, yesterday a friend wrote his C program under Windows sent to me, I gladly promised, I thought I would soon be able to run in my Linux system. Did not expect to appear garbled, the results want to solve this problem, and suddenly engaged in nearly three hours did not solve. Today, again think of this problem, unexpectedly suddenly solved, now to learn some things summarized as follows.

First of all, Windows files to get Linux to see garbled is a very common phenomenon, this information on the Internet also has a lot. But the solution is mostly similar, some concepts are not explained clearly, perhaps the individual did not understand clearly.

Try method One:

When I met this problem, I thought of a Linux class to learn a command Dos2unix, has not used before, this time did not solve the problem.

Because, this command is mainly used to deal with the difference between DOS and Linux. Dos text file is based on \ r \ n As a break mark, expressed as 16 binary is 0D 0 A. The text file under Unix is marked with \ n as a break mark, which means that the 16 binary is 0 a. Sometimes we will encounter such a situation, DOS files under the UNIX display is not a break, appear very messy, then use this command is OK.

Later on the online search, know that the encoding format is caused by inconsistency. Because DOS edit text by default is GBK storage, and Linux default to take the storage method is UTF-8, of course, will appear garbled, but how to convert dos GBK file into UTF-8?

Try method Two: Change the encoding used when Vim reads

First add a knowledge, refer to the blog http://blog.csdn.net/kl222/article/details/4550269

Basic knowledge of VIM coding:
There are 3 variables:
encoding--This option enables the use of buffered text (the file you are editing), registers, Vim script files, and so on. You can think of the ' encoding ' option as a setting for the internal operating mechanism of Vim.
fileencoding--This option is the type of encoding that VIM uses when writing to a file.
termencoding--This option represents the encoding type that is used by the output to the customer terminal (term).

So, I tried it in vim

: Set Encoding

: Set Fileencoding=utf-8 #让Vim将gbk的文本转换成utf-8, displayed

After this change, sure enough, vim to see the garbled disappeared, at this time the mood small excited suddenly.

However, when I quit vim to open the file again, garbled again appeared, originally I changed the vim setting is only temporary, so I decided to write it to the Vim configuration file, the operation is as follows:

Edit the ~/.VIMRC file, plus the following lines:

Set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936

Set Termencoding=utf-8

Set Encoding=utf-8

At this point, each time with vim to open the file does not appear garbled, this thought the problem solved, I began to compile the program, when I run, garbled or appeared .... At this time, want to cry without tears

Later, I think about it, I changed the configuration of vim, just let us see no garbled, but fundamentally, the encoding of the file is still unchanged, so when I compile with GCC, the encoding format of the file should still be GBK. This leads to garbled characters. So, I decided to find a tool to directly talk about converting GBK files into utf-8.

Try three:

1. View the file storage format

#file Test.cpp

test.cpp:iso-8859 中文版 text, with CRLF line terminators

You can see that the files are stored in ISO-8859.

2. View System Language Settings

#cat/etc/sysconfig/i18n

Lang= "en_US. UTF-8 "
Sysfont= "Latarcyrheb-sun16"
You can see that Linux is encoded in UTF-8

3. Use the tool to convert files from ISO-8859 to UTF-8 (I'm using the Iconv command here)

#iconv-f iso-8859-t UTF-8 test.c-o test_u.c

Then there was an error:

Iconv:conversion from ' ISO-8859 ' are not supported

Try ' iconv--help ' or ' iconv--usage

Solution:

Replace the ISO-8859 with GBK

#iconv-f gbk-t UTF-8 test.c-o test_u.c

Finally done!

Reference documents:

[1].http://blog.csdn.net/kl222/article/details/4550269

[2].http://www.cnblogs.com/cosiray/archive/2012/05/04/2483111.html

[3].http://blog.chinaunix.net/uid-26786246-id-3636785.html

Windows go to Linux, file garbled, file encoding conversion

Related Article

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.