Conversion problems of Java character encoding

Source: Internet
Author: User

Overview:

I think the coding problem with strings really bothers a lot of developers. I have also been troubled recently.

The problem is this, we scan the QR code to get the information in the QR code. But. Our two-dimensional code generation process is "diversified". That is, when a QR code is generated, it is encoded in a different string encoding type. For example, GBK, GB2312, UTF-8 and so on. These different encoding types produce different bytes. In Java. GBK and GB2312 are 1 Chinese characters and 2 bytes, and UTF-8 is 1 Chinese characters, which accounts for 3 bytes. The ISO code is 1 characters and 1 bytes. In this way, we scan the QR code when there will be some "enigmatical" garbled characters.

Here we are developing a two-dimensional scan. The generation of two-dimensional codes comes from different producers.


my pre-test:

System character format: UTF-8

  string" China " GB2312 encoded byte array BS_GB and UTF-8 encoded byte array bs_utf
< Span style= "font-family:"courier new"; font-size:14px ">    
Byte[] Bs_gb = { -42, -48, -71,-6};
     Byte[] Bs_utf = { -28, -72, -83, -27, -101, -67};


Experimental steps:

1. First convert the BS_GB to an intermediate result using GB2312 encoding and UTF-8 encoding respectively:
String gbStr1 = new String (BS_GB, "GB2312");
String utfStr1 = new String (BS_GB, "UTF-8");
Results:
China
? й?




    2. GB2312 encoding of UTFSTR1 for the process of GB2312 encoding from GB2312 coding, UTF-8 encoding
    String gbStr2 = new String (Utfstr1.getbytes ("UTF-8"), "GB2312");
    Result: 锟 called decyl

    3. Convert Bs_utf to an intermediate result using GB2312 encoding and UTF-8 encoding respectively:
    String gbStr3 = new String (Bs_utf, "GB2312");
    String utfStr2 = new String (Bs_utf, "UTF-8");
    Results:
    Juan??


China

4. UTF-8 encoding of GBSTR3 for the process of UTF-8 encoding from UTF-8 coding, GB2312 encoding
String utfStr3 = new String (Gbstr3.getbytes ("GB2312"), "UTF-8");
Results:
    ??

?



5. According to the above 4 steps. Perform a conversion experiment between GBK and UTF-8


Preliminary Conclusions of the experiment :

No direct conversion between UTF-8 encoding and GB2312 encoding
UTF-8 encoding and GBK encoding, only can be UTF-8, GBK, UTF-8


conversions between different encoded characters:

UTF-8 conversion to Unicode

private static final char[] Hexdigit = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '}; private static char tohex (int nibble) {return hexdigit[(nibble & 0xF)];}     /** * Encodes a string into Unicode. * @param thestring to be converted into Unicode encoded strings.

* @param escapespace whether to ignore spaces * @return Returns the converted Unicode encoded string.

*/public static string Tounicode (String TheString, Boolean escapespace) {int len = thestring.length (); int buf Len = len * 2; if (Buflen < 0) {Buflen = Integer.max_value;} StringBuffer outbuffer = new StringBuffer (Buflen); for (int x=0; x<len; x + +) {char Achar = Thestring.charat (x); Handle Common case first, selecting largest block that//avoids the specials below if ((Achar >) & & (Achar < 127)) {if (Achar = = ' \ \ ') {outbuffer.append (' \ \ '); outbuffer.append (' \ \ '); continue; } outbuffer.append (Achar); continue;} Switch (achar) {case ': if (x = = 0 | | escapespace) outbuffer.append (' \ \ '); Outbuffer.append ("); break; Case ' \ t ': outbuffer.append (' \ \ '); Outbuffer.append (' t '); break; Case ' \ n ': outbuffer.append (' \ \ '); Outbuffer.append (' n '); break; Case ' \ R ': outbuffer.append (' \ \ '); OutbuffeR.append (' R '); Case ' \f ': outbuffer.append (' \ \ '); Outbuffer.append (' F '); break; Case ' = '://Fall through case ': ':///Fall through case ' # '://Fall through Case '! ': outbuffer.append (' \ \ '); Outbuffer.append (Achar); Break;default:if ((A Char < 0x0020) | | (Achar > 0x007e)) {outbuffer.append (' \ \ '); Outbuffer.append (' U '); Outbuffer.append (Tohex (Achar >> & 0xF)) Outbuffer.append (Tohex ((Achar >> 8) & 0xF); Outbuffer . Append (Tohex ((Achar >> 4) & 0xF)); Outbuffer.append (Tohex (Achar & 0xF));} else {outbuffer.append (Achar);}}} return outbuffer.tostring ();}


Convert Unicode to UTF-8

/** * Unicode converted to Utf-8 * * @param thestring * @return */public static string UnicodeToUtf8 (String thestring) {char Achar ; int len = Thestring.length (); StringBuffer outbuffer = new StringBuffer (len); for (int x = 0; x < len;) {Achar = Thestring.charat (x + +), if (Achar = = ' \ \ ') {Achar = Thestring.charat (x + +), if (Achar = = ' U ') {//Read the Xxxxint VA Lue = 0;for (int i = 0; i < 4; i++) {Achar = Thestring.charat (x + +); switch (achar) {case ' 0 ': Case ' 1 ': Case ' 2 ': Case ' 3 ': Case ' 4 ': Case ' 5 ': Case ' 6 ': Case ' 7 ': Case ' 8 ': Case ' 9 ': value = (value << 4) + Achar-' 0 ', break;case ' a ': Case ' B ': CAs E ' C ': Case ' d ': Case ' e ': Case ' f ': value = (value << 4) + ten + Achar-' a '; Break;case ' a ': Case ' B ': Case ' C ': Case ' d ': C    Ase ' E ': Case ' F ': value = (value << 4) + + Achar-' A '; break;default:throw new IllegalArgumentException ("Malformed \\UXXXX encoding. ");}} Outbuffer.append ((char) value);} else {if (Achar = = ' t ') Achar = ' \ t '; else if (Achar = = ' r ') Achar = ' \ r '; else if (Achar = = ' n ') Achar = '\ n ', else if (Achar = = ' F ') Achar = ' \f '; outbuffer.append (Achar);}} Elseoutbuffer.append (Achar);} return outbuffer.tostring ();}

And for its GB2312 encoding format is not able to obtain a string after the conversion to other encoding format.

For example, one of my strings is "China", and I turn it into GB2312 encoding format: Juan??, then turn into UTF-8??

?

Just fine, for after GBK and iso-8859-1 These two formats encode the character, then carries on the UTF-8 transformation, is can convert back. For example, the following test:

Results:

Conversion problems of Java character encoding

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.