Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the solution to the garbled code of Linux Chinese characters?

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

What is the solution to Linux Chinese character garbled? I believe many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Under Linux, have you ever experienced that the display of Chinese characters in the text file is normal, but it becomes garbled as soon as it is inserted into the database; the file name is displayed normally on the local machine, and what is displayed on the server becomes garbled. Recently, you were almost killed by this problem, and you have gained a little bit. Here is what you have learned:

1, the first thing to know is how the file is encoded, that is, whether the file is stored in utf8 or gbk encoding, because this is fundamental. Take xxx.dat file as an example

Open the xxx.dat file with the vi command

In command line mode: entering set fileencoding will display the encoding mode of the file at the bottom of the terminal. If the result is cp936 (equivalent to gbk) or utf8, it means that the encoding mode of the file has been correctly judged. If it is latin1, we may have to set the value of the environment variable fileencodings of VI and open the file vimrc. The file of RHEL is in / etc directory and Ubuntu is in / etc/vim directory. Or you can create a ~ / .vimrc file.

Then merge the inside into: set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1 and then re-vi xxx.dat

PS: when we open the file, VIM tries to decode it sequentially using the encoding in fileencodings. If successful, use this encoding to decode, and set fileencoding to this value. If it fails, continue to try the next encoding. While latin1 is a very loose coding method, any text obtained by encoding will not fail to decode with latin1-- of course, the result of decoding is naturally "garbled".

2, the next more important is the LANG environment variable (about the meaning and function of this variable Baidu, you can find a bunch)

If xxx.dat is encoded in utf8, we need to set the environment variable LANG to LANG=zh_CN.utf8 when inserting it into the database. If xxx.dat is cp936 or LANG, we need to set it to LANG=zh_CN.gbk.

In this way, when you insert it, you don't need to use the program to convert the characters of the file, you can insert it directly.

PS: use the locale-a command to see what values LANG can be set, and locale-m to see which character sets the system supports. If locale-a doesn't have the value you need, such as zh_CN.gbk, you need to install it manually, and the installation method is simple.

A. Sudo cp / var/lib/locales/supported.d/local / var/lib/locales/supported.d/local.old [optional, backup]

B. Sudo cp / usr/share/i18n/SUPPORTED / var/lib/locales/supported.d/local

Edit / var/lib/locales/supported.d/local to remove most unused lines (locales)

C. Sudo dpkg-reconfigure locales

Edit the / etc/environment file and set up the LANG to be used

You can also use the locale-gen command to set the default locale, such as sudo locale-gen zh_CN.UTF-8

3, there is also a colleague, the LANG environment variable (here we should pay attention to the scope of the environment variable) has also been changed, the windows file has been re-transmitted to the Linux server, through web browsing, the file name is still garbled, this is because LANG has not yet taken effect on the apache server, just restart the apache service.

After reading the above, have you mastered the solution of Linux Chinese character garbled code? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report