A strange question about UNICODE character encoding

Source: Internet
Author: User

One student asked a strange question about UNICODE character encoding.

 

The problem is as follows:

String strchina = "China ";

 

(1) print the corresponding integer in each character directly, and the result is the Unicode code of the character. The following code:

For (INT I = 0; I <strchina. Length (); I ++)

{

System. Out. println (integer. tohexstring (INT) strchina. charat (I )));

}

The output is as follows:

4e2d

56fd

(2) The following code:

Byte [] Buf = strchina. getbytes ("Unicode ");

For (INT I = 0; I <Buf. length; I ++)

{

System. Out. println (integer. tohexstring (BUF [I]);

}

The output is as follows:

Ffffffff

Fffffffe

2d

4e

Fffffffd

56

What do the printed "ffffffff" and "fffffffe" indicate? Why is "2D" and "4E" opposite to the result of direct printing?

 

The answer is as follows:

 

In computer systems of different architectures, the byte storage order of Unicode characters encoded by UTF-16 in memory is different. In an Intel CPU-used computer, a multi-byte data is usually stored in the memory in the form of: Low bytes before, high bytes after, this method is called Little-Endian (the most unimportant byte comes first ). However, some computers that use other CPUs store multi-byte data in the form of high-byte front and Low-byte back, this method is called Big-Endian (the most important byte is first ). For dual-byte data such as 0x1234, use the little-Endian and big-Endian storage methods in the memory format 7.4.

 


For files encoded in UTF-16, byte order mark (BOM) is usually used to describe the byte storage order used by the characters in the file. If the file starts with 0xfe 0xff, it indicates that the rest of the text is the UTF-16 code of big-Endian; if the file starts with 0xff 0xfe, it indicates that the rest of the text is the UTF-16 encoding of little-Endian; if the beginning of the file is not marked with any byte order, it means that all the text is the UTF-16 encoding of big-Endian.

"Ffffffff" and "fffffffe" are actually two bytes of 0xff and 0xfe. When printing them as integers, they become four bytes. Because the highest bit of 0xfe is 1, when it is converted to a 4-byte integer, all the bit bits of the first three bytes are supplemented with 1, and the result is 0 xfffffffe. Printed above:

Ffffffff

Fffffffe

2d

4e

Fffffffd

56

Actually:

FF

Fe

2d

4e

FD

56

 

The first two bytes are about the byte storage order!

 

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.