Convert Java Chinese to Unicode encoding and Unicode encoding into Chinese

Source: Internet
Author: User

turn from: one leaf floating boat http://blog.csdn.net/jdsjlzx/article/details/7058823

 PackageLia.meetlucene;Importjava.io.IOException;Importorg.apache.lucene.index.CorruptIndexException; Public classUnicode { Public Static voidMain (string[] args)throwscorruptindexexception, IOException {String s= "Introduction"; String TT= Gbencoding (s);//String tt1 = "Hello, I want to tell you a thing";System.out.println ("Unicodebytes is:" +TT); //Output "Introduction" of Unicode encodingSYSTEM.OUT.PRINTLN ("corresponding Chinese:" + decodeunicode ("\\U7B80\\U4ECB"));//System.out.println (Decodeunicode (TT1)); //the output Unicode encoding corresponds to the ChineseSystem.out.println ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println (S.indexof ("\\")); }     Public StaticString gbencoding (FinalString gbstring) {        Char[] Utfbytes =Gbstring.tochararray (); String unicodebytes= "";  for(intByteindex = 0; Byteindex < Utfbytes.length; byteindex++) {String Hexb=integer.tohexstring (Utfbytes[byteindex]); if(Hexb.length () <= 2) {Hexb= "00" +Hexb; } unicodebytes= Unicodebytes + "\\u" +Hexb; }        returnunicodebytes; }     Public StaticString Decodeunicode (FinalString Datastr) {        intStart = 0; intEnd = 0; FinalStringBuffer buffer =NewStringBuffer ();  while(Start >-1) {End= Datastr.indexof ("\\u", start + 2); String Charstr= ""; if(end = =-1) {Charstr= datastr.substring (start + 2, Datastr.length ()); } Else{charstr= datastr.substring (start + 2, end); }            CharLetter = (Char) Integer.parseint (CHARSTR, 16);//16 binary parse-shaped string. Buffer.append (NewCharacter (letter). ToString ()); Start=end; }        returnbuffer.tostring (); }}

Code Explanation:

     Public StaticString Decodeunicode (FinalString Datastr) {        intStart = 0; intEnd = 0; FinalStringBuffer buffer =NewStringBuffer ();  while(Start >-1) {End= Datastr.indexof ("\\u", start + 1); //makes the first Unicode between Start~end, +1,+2,+3 can beSystem.out.println (start + "asdfasd~~~~~~~~~~~~~~~~~~~~~" +end); //the index of the first occurrence of the specified substring,//starting at the specified index,//Or-1 If there is no such occurrence.String charstr = ""; if(end = =-1) {Charstr= datastr.substring (start + 2, Datastr.length ()); } Else{charstr= datastr.substring (start + 2, end); }            CharLetter = 0; if(charstr.length () = = 4) { Letter= (Char) Integer.parseint (CHARSTR, 16);//16 binary parse-shaped string.             }            //Prevent ErrorsBuffer.append (NewCharacter (letter). ToString ()); Start=end; }        returnbuffer.tostring (); }

Java Chinese converts to Unicode encoding and Unicode encoding into Chinese

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.