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