File encoding and encoding conversion in Linux

Source: Internet
Author: User
Article Title: file encoding and encoding conversion in Linux. Linux is a technology channel of the IT lab in China. Including desktop applications, Linux system management, kernel research, embedded systems and open source and some other basic classification Windows default file format is GBK (gb2312), of course, can also be saved as a UTF-8, the simplest way is to use Notepad's "Save.
 
To be compatible with windows, the Default file encoding configuration in SecureCRT is "Default", which is actually GBK. if we change it to "UTF-8", we can see that the GBK files from scp past in windows will become garbled and need to be converted, it is more troublesome.
 
But sometimes we also need to encode the conversion, for example this time I used Chinese when setting up cacti template, apache is the UTF-8 encoding by default, so recorded in the mysql database is the UTF-8 encoding (strange is that in mysql using "set names UTF-8" can not see the correct Chinese, need to use "hex () "The function looks at the binary. count the number of characters to see if it is UTF-8 .........). When I added the server information to the database using the batch host script, I found garbled characters on the webpage. The reason is that if scripts are used in cli, the Chinese characters are saved to the database in GBK format. So turned into a very depressing situation, whether the browser encoding is set to "UTF-8" or "GB2312", there will always be a part of the Chinese is garbled.
 
The solution is not to convert the original template to gb2312, is to add the host information in the Chinese to the UTF-8. because I like UTF-8 a little bit, so I decided to use the latter method.
 
There are many ways to convert the code in linux. google, the average person is using the "iconv" command for conversion. However, because I need to judge the file encoding in the script, "iconv" is not suitable. In fact, there is another "enca" in Linux that can perform encoding conversion and is more powerful than "iconv ".
 
Debian installation:
 
Aptitude install enca
 
Usage:
 
Enca-L zh_CN file check file encoding
 
Enca-L zh_CN-x UTF-8 file to convert file encoding to "UTF-8" Encoding
 
Enca-L zh_CN-x UTF-8 <file1> file2 can do this if you don't want to overwrite the original file
 
In addition to checking the file encoding function, "enca" also has the advantage that if the file is the encoding you want to convert, it will not report an error or print the result, "iconv" reports an error. This is convenient for script writing.
 

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.