Java string encoding and conversion

Source: Internet
Author: User

/This program runs in UTF-8 encoding by default.
String A = "Zheng gaoqiang ";
String B = NULL;
B = new string (A. getbytes (), "utf8 ");
System. Out. println (B); // correctly displayed
B = new string (A. getbytes ("gb2312"), "gb2312 ");
System. Out. println (B); // displayed correctly. Although a is originally three-byte encoded by default, getbytes ("gb2312 ")
// Convert the entire byte array into two bytes. Use gb2312 to explain the new byte array.
B = new string (A. getbytes ("gb2312"), "utf8 ");
System. Out. println (B); // garbled. It has been converted into dual-byte format. If utf8 is used, an error occurs.
// I haven't thought of how to save B. It seems that B cannot be correctly displayed again.

B = new string (A. getbytes (), "gb2312 ");
System. Out. println (B); // garbled. Getbytes splits character strings one by one in UTF-8 format into n Bytes.
// The back end uses gb2312 to explain the n Bytes. It must be garbled. Utf8 three-byte, gb2312 dual-byte
B = new string (A. getbytes ("utf8"), "gb2312"); // the same as the previous sentence.

System. Out. println (B); // garbled

 

Result:

Zheng gaoqiang

Zheng gaoqiang

����

��

��

 

The key to character encoding conversion is to understand the internal mechanism... The key to encoding is to understand how the bottom byte array is encoded. For example, gb2312 represents a Chinese character in two bytes, and utf8 represents a Chinese character in three bytes, the underlying byte array must be different ~~~

 

!!! Java needs to convert character encoding: it is solved by a string. getbytes ("charsetname"). At this time, the original string byte array has been converted one by one, and the encoding has changed. For example, the original three-byte UTF-8 encoding, converted to gb2312, has become dubyte encoding, And the byte array is shorter than the original string contained array.

New String is just a process of assembling strings. If the input byte array is encoded, it should be assembled by encoding (or interpretation). Otherwise, it will be a tragedy ~~~

 

!!! Although the default encoding of the program is utf8, this does not mean that strings encoded in gb2312 cannot be correctly displayed. (This is my personal misunderstanding) because the system will automatically handle out. println. In fact, the default encoding is utf8, which only refers to getbytes or new
Utf8 is used by default for operations such as inputstreamreader.


 

Let's talk about the relationship between encoding and Character Set: For details, see another article http://www.cnblogs.com/kenkofox/archive/2010/10/15/1851962.html.

 

Finally, the JDK explains the getbytes and new string (byte [], charsetname) of string:

Public byte [] getbytes ()

Encodes this string into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

 

New String (byte [], charsetname)

Constructs a new string by decoding the specified array of bytes using the specified charset. the length of the new string is a function of the charset, and hence may not be equal to the length of the byte array.

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.