Java Chinese string encoding GBK turn UTF-8

Source: Internet
Author: User

Wrote two articles about the code, thinking that they know more about the code?!

The result was a solid lesson today.

The problem of the previous round and round solved is still a simple situation. That is iso-8859-1 turn utf-8, or iso-8859-1 to gbk,gb2312. This lossless conversion, a line of code is done.

Today met GBK turn utf-8. No matter how it turns, it is garbled.

First, the reason for garbled characters

GBK's Chinese encoding is a Chinese character denoted with "2" bytes, for example the GBK encoding of the "interior" of the Chinese character is displayed as C4 da b2 BF

Utf-8 Chinese code is a Chinese character with "3" byte representation, for example, the Chinese character "inside" of the Utf-8 code 16 binary display for E5, E9, A8

It is clear that GBK is unable to convert directly to Utf-8, few bytes into multibyte, who knows what is missing bytes?!

Ii. Methods of conversion

Is there a way to achieve a "lossy" conversion? The answer is yes.

1. First, the GBK string GetBytes () is given two primitive bytes, converted into a stream of binary characters, a total of 16 bits.

2. According to UTF-8 's Chinese encoding rules, the first byte begins with 1110, the second byte begins with 10, and the 3rd byte begins with 10. Inserts a flag bit in the original 2 binary string. The final length is from--->16+3+2+2=24.

3. Conversion complete, the actual situation needs to consider more factors, such as the string is a mixture of Chinese characters and numbers, need to identify the processing of numbers.

Three, do not repeat the invention of the wheel

This code test can also be useful, you need to refer to.

1  Public Staticstring getutf8stringfromgbkstring (String gbkstr) {2         Try {  3             return NewString (Getutf8bytesfromgbkstring (GBKSTR),"UTF-8"); 4}Catch(unsupportedencodingexception e) {5             Throw NewInternalerror (); 6         }  7     }  8       9      Public Static byte[] getutf8bytesfromgbkstring (String gbkstr) {Ten         intn =gbkstr.length ();  One         byte[] Utfbytes =New byte[3*N];  A         intK =0;  -          for(inti =0; I < n; i++) {   -             intm =Gbkstr.charat (i);  the             if(M < -&& m >=0) {   -utfbytes[k++] = (byte) m;  -                 Continue;  -             }   +utfbytes[k++] = (byte) (0xe0| (M >> A));  -utfbytes[k++] = (byte) (0x80| (M >>6) &0x3f));  +utfbytes[k++] = (byte) (0x80| (M &0x3f));  A         }   at         if(K <utfbytes.length) { -             byte[] tmp =New byte[K];  -System.arraycopy (Utfbytes,0Tmp0, K);  -             returntmp;  -         }   -         returnutfbytes;  in}

PS: A bit sorry original author, find a lot of code, one test, the results of the page is closed, if there is a big God claim, I must add links, and deep gratitude.

Java Chinese string encoding GBK turn UTF-8

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.