In-depth analysis of Chinese coding issues in the Java Web

Source: Internet
Author: User

reasons to encode a Java Web project:1. The smallest unit of information stored in a computer is 1 bytes, or 8 bits, so the range of characters that can be represented is 0~255. 2, the computer needs to represent too many symbols, can not be fully represented by 1 bytes. To solve this problem, you have to have a new data structure, char, which is encoded from bit to char.Common encoding formats:1, ASCII code a total of 128, with 1 characters of low 7 bits, 0~31 is control characters, such as line break, enter, delete and so on. 32~126 is a printed character that can be entered by the keyboard and expressed  2, Iso-8859-1iso organization on the basis of ASCII and set up a series of standards to extend the ASCII encoding, they are iso-8859-1 and iso-8859-15. The former covers most of the Western European language characters, so the use is more extensive. Iso-8859-1 is still a single-byte encoding, which can represent a total of 256 characters. Because iso-8859-1 is single-byte, many Chinese characters (two bytes) are encoded with iso-8859-1, because they are out of range, so they appear. In many cases, Chinese characters are represented by Iso-8859-1. We call it a "black hole" (it absorbs the characters you don't know). Because most of the basic Java framework or system is now the default character set encoding is iso-8859-1, so it is easy to garbled problem.  3, gb2312gb2312 is a double-byte encoding, the total encoding range is a1-f7, where a1-a9 is the symbol area, a total of 682 symbols, B0-f7 is the Chinese character area, contains 6,763 Chinese characters.  4, GBKGBK full name is the Chinese character code expansion specification, is the National Technical Supervision Bureau developed to expand GB2312, and added more Chinese characters, its coding range is 8140~fefe, can represent 21,003 Chinese characters. It is compatible with the GB2312.  5, gb18030gb18030 full name is the information technology Chinese coded character set, is China's mandatory standard, it may be single-byte, double-byte or four-byte encoding. Its encoding is compatible with GB2312 encoding.  6 and Utf-16iso try to create a new hyper-language dictionary that the world's languages can use to translate. UTF-16 specifically defines how Unicode characters are accessed in a computer, and UTF-16 uses two bytes to represent the conversion format of Unicode, which takes a fixed-length representation, that is, any character can be represented in two bytes. Two bytes is 16 bit, so called UTF-16. Java takes UTF-16 as the in-memory character storage format.  7 and Utf-8utf-8 adopt the method of variable length to overcome the disadvantage of wasting resources in UTF-16. Each coded area has its own loadline length, and different types of characters can be made up of 1~6 bytes. UTF-8 is still represented in 1 bytes for characters within a single-byte range, with 3 bytes for Chinese characters. UTF-8 encoding Rules: When it is 1 bytes, the highest bit is 0, then this is an ASCII character, thus,All ASCII codes are already UTF-8. When it is 1 bytes, beginning with 11, the number of consecutive 1 implies that the number of bytes of the character, such as 110XXXXX, is the first byte of the double-byte UTF-8 character. When it is 1 bytes, start with 10, indicating that it is not the first byte. You need to look forward to get the first byte of it.   Comparison of the above several encoding formats: GBK can handle all kanji characters, so when comparing GB2312 and GBK, GBK should be chosen. UTF-8 and UTF-16 both deal with Unicode encoding, although their encoding format is not the same, but relatively speaking, the UTF-16 encoding is more efficient, the conversion from character to Byte is simpler, the string operation is also better, suitable for disk and memory use, So Java's memory encoding is encoded using UTF-16. However, in the network transmission, should use UTF-8, because the network transmission is more prone to damage the byte stream, while the UTF-8 ASCII characters using a single-byte encoding, the other individual byte damage will not affect the other characters in the encoding efficiency intermediary between GBK and UTF-16, so, UTF-8 is an ideal Chinese coding method, which balances coding efficiency and coding security.  scenarios that need to be coded in Java: Refers to a character-to-byte or byte-to-character conversion. It is divided into the following two classes: encoding in memory operations that exist in I/O operations is encoded in Java, such as String name = "Today the weather is good"; byte[] iso8859 = name.getbytes ("iso-8859-1"); Byte[] gb2312 =name.getbytes ("GB2312"), encode the string in the appropriate encoding format, and convert it to a byte array. String. GetBytes (charsetname) encoding process: According to CharsetName find CharSet class, and then based on this character set encoding generation Charserencoder, this class is all character encoding of the parent class, For different character encoding set in its subclass defines how to implement the encoding, with the Charserencode object can be used encode method to implement the code.FAQ Analysis1, the Chinese becomes the character string that can not understand in decoding the character set and encoding used when the character set is inconsistent, it will cause the Chinese characters to read garbled, and is a Chinese character into two garbled characters. 2. A Chinese character becomes a question mark after the Chinese and Chinese symbols have been encoded by ido-8859-1 that do not support Chinese, all characters become "? ”。 This is because iso-8859-1 at the time of decoding, encountered in the code value range of characters will be uniformly 3f, which is the "black hole." and the characters that iso-8859-1 don't know become "? ”。 3, a Chinese character into two question mark Chinese after many times code, but one of the encoding or decoding is incorrect and the above situation occurs. 4, an abnormal correct encoding sometimes we will find that when you get the parameters on the JSP page, String value = Request.getparameter ("name"), garbled. Instead, use string value = String (Request.getparameter ("name". GetBytes ("Iso-8859-1"), "GBK")), and the Chinese will be displayed normally. In fact, the encoding range of the iso-8859-1 character set is 0000~00FF, which corresponds exactly to the encoding range of a single byte. This feature ensures that encoded values are not changed when encoded and decoded using ISO-8859-1. Although the Chinese characters in the network transmission process, was mistakenly split into two European character, but because of the output of the use of iso-8859-1, the results of the words were disassembled and merged, just to form a correct Chinese character. The disadvantage is to add multiple encodings at once. The solution to this problem is to set the Usebodyencodinguri configuration key to "true" in the Tomcat configuration file.website Internationalization: Converts the text of a Web site to a language form. The first thing to do is to set the encoding format of your Web site to support UTF-8 encoding for multiple languages, and then localize your pages for translation. Localization translation is divided into: Automatic machine translation, similar to Google Translate or office simplified conversion; manual translation, the cost is higher. The former is of high technical difficulty, and some semantics are difficult to translate accurately. The latter kind of system maintenance trouble. Example: from complex to simple process: by human translation of Simplified Chinese GBK encoded Chinese characters into traditional BIG5 encoded Chinese characters, form a code table. Because of the character of the characters of the characters, in two consecutive bytes of the highest bit is greater than 1 o'clock, with the two consecutive bytes together from the Code table to find, for comparison translation. This search and translation work is done dynamically on the front-end Web server, automatically converting the background output from Simplified Chinese to Traditional Chinese. But there are some phrases that are not good enough to solve cross-language problems.

In-depth analysis of Chinese coding issues 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.