Convert GBK characters into UTF-8 encoding formats using Java programs

Source: Internet
Author: User

Now I have written a Java program for your reference.

Introduction to UTF-8 coding UTF-8 coding is a widely used code that is committed to incorporating global languages into a unified code that has been incorporated into 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 in length. 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 digits of the character encoding byte. For example, the two byte character encoding styles are: 110 XXXXX 10 xxxxxx; the three-byte encoding style is: 1110 XXXX 10 xxxxxx 10xxxxxx .; similarly, the six-byte character encoding format is 1111110x 10 xxxxxx 10 xxxxxx 10 xxxxxx 10 xxxxxx 10 xxxxxx. The xxx value is filled in by characters encoded binary characters. Use only the shortest one character-encoded multi-byte string. For example: Unicode Character: 00 A9 (copyright) = 1010 1001, UTF-8 encoded as: 11000010 10101001 = 0x C2 0xa9; character 22 60 (not equal to the symbol) = 0010 0010 0110 0000, UTF-8 code: 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.