Chinese encoding in the Java Web

Source: Internet
Author: User

Java Web development often encounter Chinese coding problems, then why do I need to encode it? Because humans need to represent too many symbols, they cannot be represented in 1 bytes, and the smallest unit of information stored in a computer is 1 bytes. Therefore, you must specify the encoding rule between char and byte.

1 Common coding methods

A variety of coding methods are available in the computer, such as ASCII, Iso-8859-1, GBK, GB2312, UTF-16, UTF-8, etc.

  • ASCII Code
    • People who have learned the computer know the ASCII code, a total of 128, with a byte of the low 7-bit representation, 0~31 is the control characters such as newline return to delete, etc., 32~126 is a print character, can be entered by the keyboard and can be displayed.
  • Iso-8859-1
    • 128 characters is obviously not enough, so ISO organization on the basis of the ASCII code to develop a number of columns to extend the ASCII encoding, they are iso-8859-1~iso-8859-15, where iso-8859-1 covers most of the Western European language characters, Most widely used in all applications. Iso-8859-1 is still a single-byte encoding, which can represent a total of 256 characters.
  • GB2312
    • Its full name is "the basic set of Chinese character encoding character set of information interchange", it is a double-byte encoding, the total encoding range is a1-f7, which from a1-a9 is the symbol area, a total of 682 symbols, from B0-f7 is the Chinese character area, contains 6,763 Chinese characters.
  • GBK
    • The full name is called "Chinese character Code extension Code", is the National Technical Supervision Bureau for the Windows95 of the new Chinese character code specification, its appearance is to expand GB2312, add more Chinese characters, its coding range is 8140~fefe (remove xx7f) total 23,940 code bit, it can express 21,003 Chinese characters, its encoding is compatible with GB2312, that is, the Chinese character encoded with GB2312 can be decoded with GBK, and there will be no garbled characters.
  • GB18030
    • The full name is the "Chinese character encoding character set for information interchange", which is a mandatory standard in China, it may be single-byte, double-byte or four-byte encoding, its encoding is compatible with GB2312 encoding, although this is the national standard, but the actual application system is not widely used.
  • UTF-16
    • When it comes to UTF that must refer to Unicode (Universal Code Uniform Code), ISO tries to create a new hyper-language dictionary that all languages in the world can translate with each other. It is conceivable how complex this dictionary is, and the detailed specification of Unicode can refer to the corresponding documentation. Unicode is the basis for Java and XML, and the following details the way Unicode is stored in a computer.
    • UTF-16 specifically defines how Unicode characters are accessed in a computer. UTF-16 uses two bytes to represent the Unicode conversion format, this is a fixed-length representation, no matter what character can be expressed in two bytes, two bytes is 16 bit, so called UTF-16. UTF-16 represents a very handy character, with every two bytes representing a single character, which greatly simplifies operations in the case of string manipulation, which is a very important reason for Java to use UTF-16 as a character storage format for memory.
  • UTF-8
    • UTF-16 Unified two bytes to represent a character, although the presentation is very simple and convenient, but also has its drawbacks, there are a large number of characters with a byte can be represented now to two bytes, storage space is magnified by one times, the current network bandwidth is very limited today, this will increase network traffic , and it's not necessary. The UTF-8 uses a variable-length technique, with different loadline lengths for each coded area. Different types of characters can be made up of 1~6 bytes.
2 coded scenes

In IO operations we generally need to encode, here IO operations include disk IO, network IO, such as the following is an example of disk IO:

 Public Static voidMain (string[] args)throwsIOException {String file= "H:/name.txt"; String CharSet= "Utf-8"; //write character to byte streamFileOutputStream OutputStream =Newfileoutputstream (file); OutputStreamWriter writer=NewOutputStreamWriter (OutputStream, CharSet); Try{writer.write ("Chinese name in Hangzhou"); }        finally{writer.close (); }        //read byte stream to characterFileInputStream InputStream =Newfileinputstream (file); InputStreamReader Reader=NewInputStreamReader (InputStream, CharSet); Try {                Char[] buffer =New Char[64];                Reader.read (buffer);        SYSTEM.OUT.PRINTLN (buffer); }        finally{reader.close (); }}

In the program design IO codec, as long as we specify a unified codec charset character set, generally no problem (or codec is in the same system, all use the default character set). But if we specify that the codec is inconsistent, there will be a Chinese garbled problem. If you specify InputStreamReader in your code as GBK encoding, garbled problems will occur.

InputStreamReader Reader      new InputStreamReader (InputStream, "GBK");

The use of string types in Java is also an encoded character set that can specify byte-to-character conversions.

1 String xxx = "Hello"; 2 New String (Xxx.getbytes (), "Utf-8"); 3 System.out.println (YYY);

3 comparison of several encoding formats

The Chinese characters gbk/gb2312/utf-16/utf-8 four encoding formats can be processed, GB2312 and GBK encoding rules similar, but the GBK range is larger, it can handle all kanji character, so GB2312 and GBK should choose GBK. Both UTF-16 and UTF-8 are processing Unicode encodings, which are not the same encoding rules, and are relatively efficient at UTF-16 encoding, with a simpler character-to-byte conversion and better string manipulation. It is suitable for use between local disks and memory, and can be quickly switched between characters and bytes, such as Java's memory encoding, which is UTF-16 encoded. However, it is not suitable for transmission between the network, because the network transmission can easily damage the byte stream, once the byte stream damage will be difficult to recover, in order to compare the UTF-8 more suitable for network transmission, the ASCII character with single-byte storage, the other single character damage will not affect the other characters, encoding efficiency between GBK and UTF-16, so UTF-8 in the coding efficiency and coding security balance, is the ideal Chinese encoding method.

4 Common problems analysis

When we encounter some garbled, how should we deal with these problems? The only reason for garbled problems is that the character set that is encoded and decoded in char to byte or byte to char conversion is caused by inconsistency, because often one operation involves multiple codecs, so it is difficult to find out which link is the problem when garbled, the following is the analysis of several common phenomena.
Chinese becomes a character that can not understand

For example, the string "Amoy!" I like it! "Ìô£¡îòï²»¶£¡" as shown in the encoding process

The character set that the string uses in decoding is inconsistent with the coded character set, which causes the Chinese characters to become unreadable garbled characters, and it is a Chinese character that becomes a two garbled character.
A Chinese character becomes a question mark

For example, the string "Amoy!" I like it! "Become"?????? The encoding process is as follows:

After the Chinese and Chinese symbols have been encoded with iso-8859-1 that do not support Chinese, all characters become "? "This is because using iso-8859-1 to encode and decode a character that is not in the range of code values is uniformly used in 3f, which is what is commonly referred to as the" black hole ", all the characters that iso-8859-1 do not recognize become"? ”。

A Chinese character becomes a two question mark
For example, the string "Amoy!" I like it! "Become"???????????? The encoding process is as follows:

This is a complicated situation, the Chinese has been encoded many times, but one of the encoding or decoding is not still appear in the text character into the "? "Phenomenon, in this case, you need to look carefully at the middle of the coding link to find out where the coding error occurred.

Reference:

1. In-depth analysis of the Chinese coding problem in Java

2, "in-depth analysis of Java Web Technology Insider"

Chinese encoding in the Java Web

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.