Some notes:
Both the 1,ASCII code and the Iso-8859-1 are single-byte encodings, ASCII codes can represent 128 characters, and iso-8859-1 can represent a total of 256 characters. Can not be expressed in Chinese, if the characters or any other character that is not within the range of IOS-8859 values are uniformly represented in 3f ("?", usually referred to as "black Hole");
2,GBK compatible GB2312, is a double-byte encoding, GB2312 contains 6,763 Chinese characters, GBK contains 21,003 characters;
3,UTF-16 is a fixed-length, double-byte encoding that greatly simplifies the operation of strings, but wastes storage space. Java takes UTF-16 as memory storage format (see coded test output source line);
4,utf-8 are variable-length, and different types of characters can be made up of 1~6 bytes (Chinese characters generally 3 bytes). The UTF-8 rules are as follows:
- If it is 1 bytes, the highest bit (8th bit) is 0, which means that this is 1 ASCII characters (00~7f). Visible, all ASCII encoding is already UTF-8.
- If it is 1 bytes, starting with 11, then the number of consecutive 1 implies the number of bytes of this character, for example: 110xxxxx means that it is the first byte of a double-byte UTF-8 character.
- If it is 1 bytes, starting with 10, indicating that it is not a first byte, you need to look forward to get the first byte of the current character.
Test code:
PackageCom.test.main; Public classTestcode { Public Static voidencode () {String name= "Amoy!" I like it! "; Tohex (Name.tochararray ()); String [] CodeType= {"Iso-8859-1", "GB2312", "GBK", "UTF-16", "UTF-8"}; for(String type:codetype) {Try { byte[] bytes =name.getbytes (type); Tohex (type, bytes); } Catch(Exception e) {e.printstacktrace (); } } } Private Static voidTohex (String name,byte[] chararray) {System.out.print (String.Format ("%-15s", Name + ":")); for(inti = 0; i < chararray.length; i++) {System.out.print (String.Format ("%-4x", Chararray[i])); } System.out.println (); } Private Static voidTohex (Char[] chararray) {System.out.print (String.Format ("%-15s", "Source:")); for(inti = 0; i < chararray.length; i++) {System.out.print (String.Format ("%-4x", (int) (Chararray[i])); } System.out.println (); } Public Static voidMain (string[] args) {encode (); }}
Output:
Java character encoding test