garbled characters are often caused by the non-uniform character set, but the author of the program is another situation caused. Message + = (char) ins.read ();
Just because of this code ...
The reason is this: when casting with char, only ASCII codes between 0-127 are converted , and our Chinese (in the gbk/gb2312 character set, Chinese is represented by two bytes, And this byte is no longer 0-127 in the range of bytes through Char forced transformation out of is "?" The
My solution to this is to put each pair of bytes into the byte queue, in the array, and finally the string (bytes[]). The code is as follows
Java code
- read = Bufferedinputstream.read ();
- byte byte1 = (byte) read;
- bytes.add (byte1);
- messageread = new string (listtobytes (bytes));
The bytes in the
Code is a byte queue, and Listtobytes is the method for the queue-to-array. &NBSP
below I say the usual reasons mentioned above.
First, let me introduce some of the commonly used character sets in Java: Gbk,gb2312,ascii,iso-8859-1,utf-8.
to ensure that it is not garbled, we first have to master the number of bytes in their character set that are different characters.
GBK: English is 1 bytes, Chinese 2 bytes
GB2312: Same as gbk
ASCII: Total length 7bit. Altogether consists of 0-127 characters: accounted for 1 bytes. Cannot represent Chinese.
iso-8859-1: Total length 8bit. is an extension of ASCII: Accounted for 1 bytes
UTF-8:1 bytes in English. The Chinese is 3 bytes.
Code instance:
public static void Comparecharset () {byte[] bytes1, Bytes2, Bytes3, Bytes4, Bytes5, Bytes6, Bytes7; String string = "a horse"; try {bytes1 = string.getbytes ("GBK"); System.out.println ("With GBK encoding path byte length is" + bytes1.length); for (int i = 0; i < bytes1.length; i++) {System.out.println ("encode each byte with GBK is" + bytes1[i]); } Bytes2 = String.getbytes ("GB2312"); System.out.println ("With GB2312 encoding path byte length is" + bytes2.length); for (int i = 0; i < bytes2.length; i++) {System.out.println ("encode each byte with GB2312 is" + bytes2[i]); } Bytes3 = String.getbytes ("iso-8859-1"); System.out.println ("With iso-8859-1 encoding path byte length is" + bytes3.length); for (int i = 0; i < bytes3.length; i++) {System.out.println ("encode each byte with iso-8859-1 is" + bytes3[i]); } bytes4 = String.getbytes ("ASCII"); SYSTEM.OUT.PRINTLN ("ASCII-encoded path byte length" + bytes4.length); for (int i = 0; i < bytEs4.length; i++) {System.out.println ("ASCII-encoded each byte is" + bytes4[i]); } bytes5 = String.getbytes ("UTF-8"); System.out.println ("With UTF-8 encoding path byte length is" + bytes5.length); for (int i = 0; i < bytes5.length; i++) {System.out.println ("encode each byte with UTF-8 is" + bytes5[i]); } BYTES7 = String.getbytes ("UNICODE"); System.out.println ("With Unicode encoding path byte length" + bytes7.length); for (int i = 0; i < bytes7.length; i++) {System.out.println ("encode each byte in Unicode is" + bytes7[i]); }} catch (Exception e) {e.printstacktrace (); System.out.println ("There is an unsupported character set exists"); } }
Print out is:
With GBK encoding path byte length of 3
Encoding each byte with GBK is 97
With GBK encoding each byte is-62
With GBK encoding each byte is-19
With GB2312 encoding path byte length of 3
Encoding each byte with GB2312 is 97
With GB2312 encoding each byte is-62
With GB2312 encoding each byte is-19
With iso-8859-1 encoding path byte length of 2
Encoding each byte with Iso-8859-1 is 97
Encoding each byte with Iso-8859-1 is 63
With an ASCII encoding path byte length of 2
Encode each byte in ASCII is 97
Encode each byte in ASCII is 63
With UTF-8 encoding path byte length of 4
Encoding each byte with UTF-8 is 97
With UTF-8 encoding each byte is-23
With UTF-8 encoding each byte is-87
With UTF-8 encoding each byte is-84
With Unicode encoding path byte length of 6
Encode each byte in Unicode is-2
Encode each byte in Unicode is-1
Encoding each byte in Unicode is 0
Encoding each byte in Unicode is 97
Encode each byte in Unicode is-102
Encoding each byte in Unicode is 108
In the expression of strings, we try to ensure that the character set is consistent to not garbled ...
Java garbled problem