Sensory encoding consists of three parts:
- The byte array generation method.
- Byte array itself.
- The method for parsing byte arrays.
In general, the generation of garbled characters is the third part of the error, that is, parsing the byte array in the wrong way. For example:
Public static void test_4 (){
Try {
String str1 = "we ";
String str2 = new string (str1.getbytes ("UTF-8"), "GBK ");
String str3 = new string (str2.getbytes ("GBK"), "UTF-8 ");
System. Out. println (str1 );
System. Out. println (str2 );
System. Out. println (str3 );
Print (str1.getbytes ("UTF-8 "));
Print (str2.getbytes ("GBK "));
Print (str3.getbytes ("UTF-8 "));
} Catch (exception e ){
}
}
Output:
----------------------------------------------------------------------------------
We
Smooth
We
-26,-120,-111,-28,-69,-84,
-26,-120,-111,-28,-69,-84,
-26,-120,-111,-28,-69,-84,
But this round-trip conversion is not always successful, because GBK represents a Chinese character requires two bytes, while the UTF-8 requires three bytes, so if it is an odd number of Chinese characters when converted to the UTF-8 has to be aligned, and then turn back of course is not the same. In this example, we can see what the getbytes and new string methods do.
First look at the ISO-8859-1 encoding, which is the most disturbing of these days. It is the default character set of most browsers. The first 128 characters correspond to the ASCII character set. The last 128 characters contain characters used by Western European countries and some common special characters, is an ASCII superset. The encoding table is as follows:
The code for converting a piece of code from ISO-8859-1 to UTF-8 obtained online is as follows:
public static byte[] code8859toUTFNew(byte[] src){
byte [] dest = new byte[src.length*2 +1];
int i = 0, j = 0;
int value;
for(i =0; i < src.length;i++){
value = src[i]& 0xFF;
if (value <= 0x7F){
dest[j++] = src[i];
}else if(value >0x7F && value <= 0xFF){
dest[j++] = (byte)((value>>6)& 0x1F|0xC0);
dest[j++] = (byte)(value&0x3F|0x80);
}
}
byte [] end = new byte[j];
System.arraycopy(dest, 0, end, 0, j);
return end;
}
UTF-8 code:
A UTF-8 is a variable-length character code for Unicode and a prefix that represents any character in the Unicode standard, and the first byte in the encoding is compatible with ASCII. The UTF-8 rules are as follows:
- 128 US-ASCII characters require only one byte encoding (from u + 0000 to U + 007f ).
- The signed Latin, Greek, Spanish, Spanish, Hebrew, Arabic, and Syrian letters are encoded in two bytes (UNICODE ranges from u + 0080 to U + 07ff ).
- Other characters in the Multi-text plane are encoded in three bytes.
- The Unicode secondary flat characters that are rarely used are encoded in four bytes.
IETF requires that all Internet protocols must support UTF-8 encoding. The encoding rules are as follows:
| Code Scope |
UTF-8 |
Note |
| 000000-00007f |
0 zzzzzzz |
ASCII character range, starting from 0 |
| 201780-0007ff |
110yyyyy10zzzzzz |
The first byte starts with 110, And the next byte starts with 10. |
|
000800-00d7ff
00e000-00ffff |
1110xxx10yyyyyy10zzzzzz |
The first byte starts from 1110, And the next byte starts from 10. |
| 00000-10ffff |
11110www10xxxxxx10yyyyyy10zzzzzz |
The first byte starts from 11110, And the next byte starts from 10. |