JAVA character encoding

Source: Internet
Author: User

AboutJAVA character encoding


Concepts and skills of character encoding

In Java-based programming, we often encounter Chinese character processing and display problems.

The default encoding method of Java is UNICODE, which brings vitality and wider adaptability to JAVA, however, the problem also arises. The files and databases we normally use are generally encoded Based on GB2312 or BIG5, how can we properly select the Chinese character encoding method and correctly process the Chinese character encoding?

English characters are generally expressed in one byte. The most common encoding method is ASCII. However, a single byte can only distinguish between 256 characters and thousands of Chinese characters. Therefore, it is expressed in Double Bytes. to separate it from English characters, the maximum bit of each byte must be 1, in this way, the double byte can contain up to 64 k characters. We often encounter encoding methods such as GB2312, BIG5, and UNICODE. For details about the specific encoding method, interested readers can refer to the relevant information. GB2312 is the country code. The value of the first byte (high byte) is the area code value plus 32 (20 H), and the second byte (low byte) the value is the bid value plus 32 (20 H). These two values are used to represent the encoding of a Chinese character.
UNICODE code is a multi-byte and other long encoding method proposed by Microsoft to solve the problem of multi-country characters. It adopts the "0" Byte prior to English characters to achieve long compatibility. For example, if the ASCII code of "A" is 0x41, UNICODE is 0 x, and 0x41, various encodings can be converted to each other using special tools.

We often encounter such a situation: the JSP-based website is garbled, and the file is garbled after it is opened, because the external file-> Java bytecode->; virtual Machine> operating system> display device. First, let's take a look at an example of JSP to solve Chinese problems. I think everyone is familiar with this code,
But what does it mean? Why?

String s1 = request. getParameter ("keyword ");
// Obtain the parameter s1 from the request

Byte [] bytes = s1.getBytes ("ISO-8859-1 ");
// Convert s1 (by Unicode-> ISO-8859-1) back to the original byte [].

String s2 = new String (bytes, "GBK ");
// Because the original parameters (or characters, strings) are encoded in GBK format
// Transcoding by GBK-> Unicode

Why? Because the received parameters are originally encoded in GBK format, the default value is
Iso-8859-1-> Unicode transcoding converts a parameter to a String
So it must be garbled (In Chinese)

As long as you understand this basic principle, the problem of Chinese characters can be solved!

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.