Java can be widely used and its internationalization capabilities are inseparable. internationalized coding is the most important part of Java internationalization, java's International encoding capability is directly related to its unicode encoding. In Java, data of any character type is represented by Unicode characters in Java programs, such as char and string. Note: The lab environment in this article is Windows XP Chinese edition and jdk1.5.
1. getbytes ()When a Java program obtains a string from an input stream, file, or number of characters, it performs character encoding conversion. For example, you must specify the encoding method in the constructor of inputstreamreader, when obtaining strings from files and characters, the system uses the default encoding method to decode character data. Consider the following code:
| String STR = "medium "; |
① |
| Byte [] bytes = Str. getbytes (); |
② |
| Bytes = Str. getbytes (ISO-8859-1 "); |
③ |
Statement ①: assign a string containing only one character "medium" to an object of the string class STR, the character "medium" is encoded according to the default encoding method of the operating system. In Chinese Windows, it is usually "GBK", and "medium" is 0xd6d0 in GBK encoding, when this character is assigned to STR, Java will encode and convert the string, that is, converting the "medium" of the GBK encoding method to the "medium" of the Unicode encoding method ", the Unicode encoding method "in" is 0x4e2d, so the binary value in the memory of STR during program running is represented as hexadecimal value: 0x4e2d. Statement ②: Obtain the binary form of the STR string. The getbytes (string encoding) method must specify the encoding method to obtain the binary form of the string. If no parameter is set in this statement, the default encoding method of the operating system is used. That is, the bytes obtained here is a binary form of "medium" in GBK encoding, that is, bytes [0] = 0xd6, bytes [1] = 0xd0. Statement ③: the difference between the statement and statement ② is that the encoding method is specified, here the ISO-8859-1 is specified, that is, Latin-1, the encoding uses 8-bit character encoding, therefore, the encoding space only contains 256 characters. This encoding only contains the basic ASCII code and some extended Western European characters. Therefore, this character set cannot contain Chinese characters, that is to say, Java Virtual Machine can not find the corresponding encoding of the word "medium" in the ISO-8859-1 encoding set, in this case, only return a question mark (?, So bytes. length is only 1 and bytes [0] = 0x3f.
Ii. New String (byte [] bytes, string encoding)The getbytes () method obtains a binary byte array from a string. To obtain a string from a binary byte array, use the new string (byte [] bytes, string encoding) method, this method parses the binary array in bytes according to the encoding method to generate a new String object.
| Byte [] bytes = {(byte) 0xd6, (byte) 0xd0, (byte) 0x31 }; |
① |
| String STR = new string (bytes ); |
② |
| STR = new string (bytes, "ISO-8859-1 "); |
③ |
Statement ①: defines a byte array. Statement ②: encode the binary data in the byte array into a string according to the default encoding method (GBK). We know that 0xd6 0xd0 in GBK indicates "medium ", 0x31 represents the character "1" (GBK is compatible with ASCII, but not the part of the ISO-8859-1 except ASCII), so the STR value is "medium 1 ". Statement ③: This sentence uses iso-8859-1 encoding method to encode the byte data, because in the ISO-8859-1 encoding method, a byte will be parsed into a character, therefore, the byte array is interpreted as a string containing three characters, but because there is no character corresponding to 0xd6 and 0xd0 in the ISO-8859-1 encoding method, the first two characters will produce two question marks, since 0x31 corresponds to the character "1" in ISO-8859-1 encoding (the ISO-8859-1 is also compatible with ASCII), the STR value obtained by this statement is "?" 1 ".
Iii. encoding conversionThe two statements described above are the basic statements to solve the coding problem in Java, and the general garbled problem can also be solved by using the above two statements. For example, converting character data encoded in UTF-8 in inputstream into a byte array encoded in GBK:
| Public byte [] Transmit (inputstream utfistream ){ |
| Bufferedreader reader =NewBufferedreader (NewInputstreamreader (utfistream), "UTF-8 "); |
| Stringbuffer content = new stringbuffer (); |
| String temline; |
| While(Temline = reader. Readline ())! =Null) |
| { |
| Content. append (temline + "/N "); |
| } |
| Return content. tostring (). getbytes ("GBK "); |
| } |
Iv. MySQL garbled
The default character encoding method in MySQL is Latin1, that is, ISO-8859-1, and the default character encoding method in Java is Unicode, so when accessing and accessing MySQL in Java, there will be Chinese garbled problem. Solution: Before MySQL is stored, convert Chinese characters from GBK encoding to Latin1 encoding. After obtaining data from MySQL, convert Latin1 encoding to GBK encoding. The conversion function is as follows:
Public static string gbktolatin1 (string Str)
{
If (STR = NULL)
{
STR = "";
}
Else {
Try {
STR = new string (Str. getbytes ("GBK"), "ISO-8859-1 ");
}
Catch (exception ex ){
Ex. printstacktrace ();
}
}
Return STR;
}
Public static string latin1togbk (string Str)
{
Try {
STR = new string (Str. getbytes ("ISO-8859-1"), "GBK ");
Return STR;
} Catch (unsupportedencodingexception ex ){
System. Out. println (Ex );
Return "";
}
}