This work is licensed using the "knowledge sharing signature"-"non-commercial use"-2.5 mainland China License Agreement in the same way.
Java string is the most common class in Java API. This article will talk about the internal principle of the string class, and describe the unique use of the ISO-8859-1 character set in string processing.
Java string internal Encoding
The string class manages an array of the char type. The Java API describes the basic char type as follows:
The Char data type (and the value encapsulated by the character object) is defined as a fixed-width 16-bit entity based on the original Unicode specification.
We can confirm this through the following statement:
System. Out. println (character. size); // The result is 16.
According to the construction method of the string class, we can define the string as follows:
String STR = "ABC ";
The above code is equivalent:
Char data [] = {'A', 'B', 'C'}; <br/> string STR = new string (data );
In addition, the basic types of char are the same as those of byte, short, Int, and long. Therefore, the above Code is equivalent:
Char data [] = {0x61, 0x62, 0x63}; <br/> string STR = new string (data );
It should be noted that the basic char type always uses 16 bits (two bytes) to represent a character, even if the Unicode value of this character is less than 0xff (such as ASCII code ). For Unicode characters with a unicode value greater than 0xff, for example, the Unicode encoding of the "China" character is/u4e2d and/u56fd respectively, we can create the following:
Char data [] = {'/u4e2d', '/u56fd'}; <br/> string STR = new string (data );
You can also create one as follows:
Char data [] = {0x4e2d, 0x56fd}; <br/> string STR = new string (data );
The preceding descriptions can clarify two key points:
Each element of a string always occupies two bytes. One or two elements (the supplementary character occupies two elements) represent one character.
Each element of a string object is encoded using the Unicode Character Set.
Character Set and byte character data
Before the emergence of Unicode specifications, computers experienced two stages of ASCII and ANSI encoding in dealing with strings. In the ASCII era, computers could only process english numbers and several basic symbols.Single-byte character set (sbcs).
In order to be able to process their own texts on computers, various countries have formulated corresponding national standards and set their respective ANSI codes. For example, the GBK standard is used for simplified Chinese, the big5 standard for traditional Chinese, and the shift_jis standard for Japanese. In the ANSI coding age, computers useMulti-byte character set (MBCS)Process text. For example, in the gb2312 standard, the Chinese characters are represented in two bytes, while the English characters "ABC" are represented in one byte. The ANSI codes of texts in different countries are not commonly used. An ANSI Code cannot be used to express texts in multiple countries.
For the sake of smooth text communication, that is to say, in a text, there can be both simplified Chinese characters and traditional Chinese characters. According to the characteristics of languages in various countries, international organizations use two bytes of data to integrate text information from most countries into a character set. This is Unicode encoding, also known as Wanguo code. The features of Unicode encoding are described in the previous article.Dubyte character set (DBCS)Process text.
In Java, you can use byte arrays to save character values of different character sets. For example, you can use the ASCII character set to save "ABC" as follows:
// Use the ASCII encoded "ABC" <br/> byte [] ascbytes = {(byte) 0x61, (byte) 0x62, (byte) 0x63 };
You can use the GBK character set to save the Chinese character as follows:
// Use GBK-encoded "China" <br/> byte [] gbkbytes = {(byte) 0xd6, (byte) 0xd0, (byte) 0xb9, (byte) 0xfa };
Of course, we can also use the Unicode Character Set to save the word "China. For example:
// Use unicode encoded "China" <br/> byte [] unicodebytes = {(byte) 0x4e, (byte) 0x2d, (byte) 0x56, (byte) 0xfd };
A special character set UTF-8 is a multi-byte representation character set that corresponds to the Unicode specification. For example, the UTF-8 of the word "medium" is encoded as "0xe4, 0xb8, 0xad" Three bytes.
Here, the byte data streams corresponding to specific character sets are calledByte Character dataIn stark contrast to the string object, the minimum unit of the string object is two bytes, and the minimum unit of the byte character data is one byte. Therefore, we can clarify the other two points:
Each element in the byte character data always occupies one byte length. One or more elements represent one character.
Byte Character data must correspond to a character set.
Exchange of byte character data and string objects
During Java program running, string objects are always stored in the memory in Unicode encoding mode. However, when a String object is saved to a persistent Resource (file or database) or transmitted over the network, generally, data is processed in byte mode. In this way, Java APIs must provide the functions of interchange between the two. In fact, this function is provided in the string class and charset class.
On the one hand, we can use the getbytes () method of the string class to return the byte character data of different character sets. Its essence is the conversion process from UNICODE character set encoding to other character sets encoding. For example:
Public static void main (string [] ARGs) {</P> <p> string STR = "China"; <br/> printbytes ("Chinese unicode encoding :", str. getbytes (charset. forname ("Unicode"); <br/> printbytes ("GBK encoding in China:", str. getbytes (charset. forname ("GBK"); <br/> printbytes ("China's UTF-8 Code:", str. getbytes (charset. forname ("UTF-8"); <br/>}</P> <p> Public static void printbytes (String title, byte [] data) {<br/> system. out. println (title); <br/> for (byte B: Data) {<br/> system. out. print ("0x" + tohexstring (B) + ""); <br/>}< br/> system. out. println (); <br/>}</P> <p> Public static string tohexstring (byte value) {<br/> string TMP = integer. tohexstring (Value & 0xff); <br/> If (TMP. length () = 1) {<br/> TMP = "0" + TMP; <br/>}</P> <p> return TMP. touppercase (); <br/>}
The output result of the above example is:
Unicode encoding in China:
0xfe 0xff 0x4e 0x2d 0x56 0xfd
GBK encoding in China:
0xd6 0xd0 0xb9 0xfa
China's UTF-8 code:
0xe4 0xb8 0xad 0xe5 0x9b 0xbd
Note that a BOM (byteordermark) exists at the beginning of the unicode encoded byte data extracted from a string object. Generally, the BOM value is "0xfe 0xff", that is, big_endian ). If the BOM value is "0xff 0xfe", it is a small byte order (little_endian ).
On the other hand, we can also use the construction method of the string class to generate a String object based on the byte character data of different character sets. Its essence is the conversion process from encoding of other character sets to encoding of UNICODE character sets. For example:
Byte [] unicodebytes = {(byte) 0x4e, (byte) 0x2d, (byte) 0x56, (byte) 0xfd}; <br/> system. out. println (new string (unicodebytes, charset. forname ("Unicode"); </P> <p> byte [] gbkbytes = {(byte) 0xd6, (byte) 0xd0, (byte) 0xb9, (byte) 0xfa }; <br/> system. out. println (new string (gbkbytes, charset. forname ("GBK"); </P> <p> byte [] utf8bytes = {(byte) 0xe4, (byte) 0xb8, (byte) 0xad, (byte) 0xe5, (byte) 0x9b, (byte) 0xbd };< br/> system. out. println (new string (utf8bytes, charset. forname ("UTF-8"); </P> <p>
In the preceding example, all three output statements output the word "China.
These two conversion processes, especially the conversion from UNICODE character set encoding to other character sets encoding, may fail. When the conversion fails, the Unicode code is automatically replaced by 0x3f. For example:
Public static void main (string [] ARGs) {</P> <p> string STR = "China"; <br/> printbytes ("Chinese big5 encoding :", str. getbytes (charset. forname ("big5"); <br/>}</P> <p> Public static void printbytes (String title, byte [] data) {<br/> // the same example above <br/>}< br/>
The output result of the above example is:
Big5 encoding in China:
0xa4 0xa4 0x3f
The "country" is represented by 0x3f because it does not have the encoding value corresponding to the big5 Character Set of traditional Chinese characters.
Special Character Set for ISO-8859-1)
ISO-8859-1 is a single-byte character set that complements the ASCII character set. The interchange of string objects and byte character data using the ISO-8859-1 character set is typically exactly the same as described above. For example:
Public static void main (string [] ARGs) {<br/> // string "ABC" <br/> byte [] bytes = {(byte) 0x61, (byte) 0x62, (byte) 0x63 }; <br/> string STR = new string (bytes, charset. forname ("ISO-8859-1"); <br/> printbytes ("ISO-8859-1 encoding:", str. getbytes (charset. forname ("ISO-8859-1"); <br/> printbytes ("unicode encoding:", str. getbytes (charset. forname ("Unicode"); <br/>}</P> <p> Public static void printbytes (String title, byte [] data) {<br/> // same as above <br/>}
The output result of the above example is:
ISO-8859-1 code:
0x61 0x62 0x63
Unicode encoding:
0xfe 0xff 0x00 0x61 0x00 0x62 0x00 0x63
From this example, we can see that the process of converting from the ISO-8859-1 character set to the Unicode Character Set Is to save each byte type element in the byte type data directly into a char type element. That is, the following code:
Byte [] bytes = {(byte) 0x61, (byte) 0x62, (byte) 0x63}; <br/> string STR = new string (bytes, charset. forname ("ISO-8859-1 "));
It is equivalent:
Byte [] bytes = {(byte) 0x61, (byte) 0x62, (byte) 0x63 }; <br/> char [] TMP = new char [bytes. length]; <br/> for (INT I = 0; I <bytes. length; I ++) {<br/> TMP [I] = (char) bytes [I]; <br/>}< br/> string STR = new string (TMP );
It should be noted that the conversion process from ISO-8859-1 to Unicode is an effective conversion between the encoded value 0x00-0xff. In the ISO-8859-1 Character Set, 0x00-0x1f, 0x7f, 0x80-0x9f are not defined. We can test with several invalid codes:
Public static void main (string [] ARGs) {<br/> // invalid ISO-8859-1 Code <br/> byte [] bytes = {(byte) 0x00, (byte) 0x1a, (byte) 0x7f, (byte) 0x93 }; <br/> string STR = new string (bytes, charset. forname ("ISO-8859-1"); <br/> printbytes ("ISO-8859-1 encoding:", str. getbytes (charset. forname ("ISO-8859-1"); <br/> printbytes ("unicode encoding:", str. getbytes (charset. forname ("Unicode"); <br/>}</P> <p> Public static void printbytes (String title, byte [] data) {<br/> // same as above <br/>}
The output result of the above example is:
ISO-8859-1 code:
0x00 0x1a 0x7f 0x93
Unicode encoding:
0xfe 0xff 0x00 0x00 0x00 0x1a 0x00 0x7f 0x00 0x93
Based on this feature, we can summarize the last key points:
Using the ISO-8859-1 character set, we can lossless save any byte array to a string object.
That is to say, you can use this feature to directly store the original bytes of the byte character data (instead of the data converted by the Unicode Character Set) in the string object. Otherwise, you can retrieve the original byte data from a ISO-8859-1-encoded String object. For example:
Public static void main (string [] ARGs) {<br/> byte [] utf8bytes = {(byte) 0xe4, (byte) 0xb8, (byte) 0xad, (byte) 0xe5, (byte) 0x9b, (byte) 0xbd }; <br/> printbytes ("original byte stream:", utf8bytes ); </P> <p> // ISO-8859-1 Encoding Process <br/> // stores the original byte data stream (without unicode encoding) to String object <br/> string isostr = new string (utf8bytes, charset. forname ("ISO-8859-1 ")); </P> <p> // ISO-8859-1 decoding process <br/> // obtain the original byte data stream that is exactly the same as utf8bytes content from the string object <br/> byte [] TMP = isostr. getbytes (charset. forname ("ISO-8859-1"); <br/> printbytes ("converted byte stream:", TMP ); <br/>}</P> <p> Public static void printbytes (String title, byte [] data) {<br/> // same as above <br/>}
The output result of the above example is:
Original byte stream:
0xe4 0xb8 0xad 0xe5 0x9b 0xbd
Convert byte streams:
0xe4 0xb8 0xad 0xe5 0x9b 0xbd
This method is used in many places to store original bytes of data through string objects. The most common is the processing of form data from the server by the Web server in Java Web applications. For details about this, refer to how to solve the garbled problem in Java Web applications.