UTF-8 coding is a widely used code that aims to incorporate global languages into a unified code.
currently, it has incorporated several Asian languages. UTF stands for the UCS Transformation format.
the UTF-8 uses variable length bytes to represent characters, theoretically up to 6 bytes.
the UTF-8 code is compatible with asc ii (0-127), that is, the UTF-8 code for asc ii characters is the same as asc ii.
for a character exceeding the length of one byte, the following encoding specifications are used:
the number of the first byte 1 on the left represents the number of bytes encoded by this character,
for example,
the single-byte encoding style is 0 xxxxxxx
the two-byte character encoding style is 110 XXXXX 10 xxxxxx;
the Three-byte encoding format is 1110 XXXX 10 xxxxxx 10xxxxxx .;
Similarly, the six-byte character encoding style is 1111110x 10 xxxxxx 10 xxxxxx 10 xxxxxx 10 xxxxxx 10 xxxxxx.
the xxx value is filled in by the binary representation of the character encoding. Use only the shortest one character-encoded multi-byte string.
Unicode characters that actually represent ASCII characters are encoded into 1 byte and the UTF-8 representation is the same as the ASCII character representation. Converting all other uncode characters to UTF-8 requires at least 2 bytes. Each byte starts with a code-changing sequence. The first byte consists of a unique code-changing sequence, which consists of N-bit 1 and one-bit 0. N-bit 1 indicates the number of bytes required for character encoding.
Each encoding character in the UTF-8 cannot start with "10", and "10" appears in the form of a connector starting with the encoding byte. Therefore, UTF-8 encoding is not prone to errors during storage and transmission.
example:
Unicode Character: 00 A9 (copyright) = 1010 1001 11000010,
UTF-8 code: 10101001 = 0x C2 0xa9;
character 22 60 (not equal to the symbol) = 0010 0010 0110 0000,
the UTF-8 is encoded: 11100010 10001001 10100000 = 0xe2 0x89 0xa0
package COM. lang. string;
Public class converfromgbktoutf8 {
Public static void main (string [] ARGs ){
Try {
Converfromgbktoutf8 convert = new converfromgbktoutf8 ();
Byte [] fullbyte = convert. gbk2utf8 (Chenese );
String fullstr = new string (fullbyte, "UTF-8 ");
System. Out. println ("string from GBK to UTF-8 byte:" + fullstr );
} Catch (exception e ){
E. printstacktrace ();
}
}
Public byte [] gbk2utf8 (string Chenese ){
Char C [] = Chenese. tochararray ();
Byte [] fullbyte = new byte [3 * C. Length];
For (INT I = 0; I <C. length; I ++ ){
Int M = (INT) C [I];
String word = integer. tobinarystring (m );
// System. Out. println (Word );
Stringbuffer sb = new stringbuffer ();
Int Len = 16-word. Length ();
// Add zero
For (Int J = 0; j <Len; j ++ ){
SB. append ("0 ");
}
SB. append (Word );
SB. insert (0, "1110 ");
SB. insert (8, "10 ");
SB. insert (16, "10 ");
// System. Out. println (sb. tostring ());
String S1 = sb. substring (0, 8 );
String S2 = sb. substring (8, 16 );
String S3 = sb. substring (16 );
Byte b0 = integer. valueof (S1, 2). bytevalue ();
Byte b1 = integer. valueof (S2, 2). bytevalue ();
Byte b2 = integer. valueof (S3, 2). bytevalue ();
Byte [] BF = new byte [3];
BF [0] = b0;
Fullbyte [I * 3] = BF [0];
BF [1] = b1;
Fullbyte [I * 3 + 1] = BF [1];
BF [2] = B2;
Fullbyte [I * 3 + 2] = BF [2];
}
Return fullbyte;
}
}
The encoding principle and characteristics of UTF-8:
U + 0000 ~ U + 007e 1 _ (7 bits)
U + 0080 ~ U + 07ff 1 1 0 _ 1 0 _ (11 bits)
U + 0800 ~ U + FFFF 1 1 0 _ 1 0 _ 1 0 _ (16 bits)