What about character encoding (Unicode, UTF-8, UTF-16) in Java

Source: Internet
Author: User

First look at the program under the character (test English and Chinese in Unicode, UTF-8, UTF-16, the three encoding, a character occupies several bytes)


[Java]
System. out. println ("a (Unicode):" + "a". getBytes ("Unicode"). length );
System. out. println ("a (Unicode):" + "aa". getBytes ("Unicode"). length );
System. out. println ("Ah (Unicode):" + "ah". getBytes ("Unicode"). length );
System. out. println ("Ah (Unicode):" + "ah". getBytes ("Unicode"). length );
System. out. println ("");
System. out. println ("a (UTF-8):" + "a". getBytes ("UTF-8"). length );
System. out. println ("aa (UTF-8):" + "aa". getBytes ("UTF-8"). length );
System. out. println ("Ah (UTF-8):" + "ah". getBytes ("UTF-8"). length );
System. out. println ("Ah (UTF-8):" + "ah". getBytes ("UTF-8"). length );
System. out. println ("");
System. out. println ("a (UTF-16):" + "a". getBytes ("UTF-16"). length );
System. out. println ("aa (UTF-16):" + "aa". getBytes ("UTF-16"). length );
System. out. println ("Ah (UTF-16):" + "ah". getBytes ("UTF-16"). length );
System. out. println ("Ah (UTF-16):" + "ah". getBytes ("UTF-16"). length );

System. out. println ("a (Unicode):" + "a". getBytes ("Unicode"). length );
System. out. println ("a (Unicode):" + "aa". getBytes ("Unicode"). length );
System. out. println ("Ah (Unicode):" + "ah". getBytes ("Unicode"). length );
System. out. println ("Ah (Unicode):" + "ah". getBytes ("Unicode"). length );
System. out. println ("");
System. out. println ("a (UTF-8):" + "a". getBytes ("UTF-8"). length );
System. out. println ("aa (UTF-8):" + "aa". getBytes ("UTF-8"). length );
System. out. println ("Ah (UTF-8):" + "ah". getBytes ("UTF-8"). length );
System. out. println ("Ah (UTF-8):" + "ah". getBytes ("UTF-8"). length );
System. out. println ("");
System. out. println ("a (UTF-16):" + "a". getBytes ("UTF-16"). length );
System. out. println ("aa (UTF-16):" + "aa". getBytes ("UTF-16"). length );
System. out. println ("Ah (UTF-16):" + "ah". getBytes ("UTF-16"). length );
System. out. println ("Ah (UTF-16):" + "ah". getBytes ("UTF-16"). length );
The running result is as follows:

A (Unicode): 4
A (Unicode): 6
AH (Unicode): 4
AH (Unicode): 6

A (UTF-8): 1
Aa (UTF-8): 2
AH (UTF-8): 3
AH (UTF-8): 6

A (UTF-16): 4
Aa (UTF-16): 6
AH (UTF-16): 4
AH (UTF-16): 6

 


You can see the situation of UTF-8: an English character occupies one byte, a Chinese character occupies three bytes

But Unicode and UTF-16 is strange, whether it is English or Chinese characters, it cannot take up to a few bytes. In fact, the correct answer is: Unicode and UTF-16 encoding, whether it is English or Chinese characters, both occupy two bytes (as for the above results, the two extra bytes are the default bytes used to represent the byte order ). As for why, continue to look down.

 


The recommended method for marking byte order in Unicode specifications is BOM. BOM is not a "Bill Of Material" BOM, but a Byte Order Mark.

(Unicode is a character encoding method, but it is designed by international organizations to accommodate all languages and texts in the world. The Unicode name is "Universal Multiple-Octet Coded Character Set", which is short for UCOS. UCOS can be seen as the abbreviation of "Unicode Character Set .)

There is a character named "zero width no-break space" in the UCS encoding, and its encoding is FEFF. FFFE does not exist in the UCS, so it should not appear in actual transmission. We recommend that you transmit the character "zero width no-break space" before transmitting a byte stream in the UCS specification ".

In this way, if the receiver receives FEFF, it indicates that the byte stream is Big-Endian; if it receives FFFE, it indicates that the byte stream is Little-Endian. Therefore, the character "zero width no-break space" is also called BOM.

When Unicode transcoding is used directly in Java, it is split as a UTF-16LE and BOM is added. If you use a UTF-16 split, A UTF-16BE split with BOM is used by default in Java.

 


Let's look at a program:


[Java]
Public class Test {
Private final static char [] HEX = "0123456789 abcdef". toCharArray ();
 
Public static void main (String [] args) throws UnsupportedEncodingException {
String str = "China ";
String [] encoding = {"Unicode", "UnicodeBig", "UnicodeLittle", "UnicodeBigUnmarked ",
"UnicodeLittleUnmarked", "UTF-16", "UTF-16BE", "UTF-16LE "};

For (int I = 0; I <encoding. length; I ++ ){
System. out
. Printf ("%-22 s % n", encoding [I], bytes2HexString (str. getBytes (encoding [I]);
}
}
 
Public static String bytes2HexString (byte [] bys ){
Char [] chs = new char [bys. length * 2 + bys. length-1];
For (int I = 0, offset = 0; I <bys. length; I ++ ){
If (I> 0 ){
Chs [offset ++] = '';
}
Chs [offset ++] = HEX [bys [I]> 4 & 0xf];
Chs [offset ++] = HEX [bys [I] & 0xf];
}
Return new String (chs );
}
}

Public class Test {
Private final static char [] HEX = "0123456789 abcdef". toCharArray ();

Public static void main (String [] args) throws UnsupportedEncodingException {
String str = "China ";
String [] encoding = {"Unicode", "UnicodeBig", "UnicodeLittle", "UnicodeBigUnmarked ",
"UnicodeLittleUnmarked", "UTF-16", "UTF-16BE", "UTF-16LE "};

For (int I = 0; I <encoding. length; I ++ ){
System. out
. Printf ("%-22 s % n", encoding [I], bytes2HexString (str. getBytes (encoding [I]);
}
}

Public static String bytes2HexString (byte [] bys ){
Char [] chs = new char [bys. length * 2 + bys. length-1];
For (int I = 0, offset = 0; I <bys. length; I ++ ){
If (I> 0 ){
Chs [offset ++] = '';
}
Chs [offset ++] = HEX [bys [I]> 4 & 0xf];
Chs [offset ++] = HEX [bys [I] & 0xf];
}
Return new String (chs );
}
}
The running result is as follows:

Unicode fe ff 4e 2d 56 fd
UnicodeBig fe ff 4e 2d 56 fd
UnicodeLittle ff fe 2d 4e fd 56
UnicodeBigUnmarked 4e 2d 56 fd
UnicodeLittleUnmarked 2d 4e fd 56
UTF-16 fe ff 4e 2d 56 fd
UTF-16BE 4e 2d 56 fd
UTF-16LE 2d 4e fd 56

 


We can see that the byte sequence of several different Unicode and UTF-16 encoding is different, some are fe ff, some are ff fe, some are not.

 


In summary:

Unicode and UTF-16: 1 character occupies 2 bytes (no matter which language)

UTF-8: 1 English character occupies 1 byte, a Chinese character (including Japanese and Korean) occupies 3 bytes

Char in Java adopts Unicode encoding by default, so char occupies 2 bytes in Java.


In addition, here is a knowledge point: one byte occupies 8 bits)

 


Author: tianjf0514

Related Article

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.