Simple implementation of Unicode and Chinese conversion in Java _java

Source: Internet
Author: User

The

looks like this:

Package test.com.gjob.services;
   Import java.util.Properties; 
      public class Test {public static void main (string[] args) {String s = "Introduction"; 
  String TT = gbencoding (s);
      String tt1 = "Hello, I want to tell you one thing"; 
  System.out.println (Decodeunicode ("\\U7B80\\U4ECB")); 
      System.out.println (Decodeunicode (TT1));
      System.out.println (Htmldecoder.decode ("China"));
      String S1 = "\U7B80\U4ECB";
     System.out.println (S.indexof ("\")); 
        public static string gbencoding (final String gbstring) {char[] utfbytes = Gbstring.tochararray (); 
        String unicodebytes = ""; for (int byteindex = 0; Byteindex < utfbytes.length; byteindex++) {String Hexb = integer.tohexstring (utfby 
            Tes[byteindex]); 
           if (Hexb.length () <= 2) {Hexb = "+" + Hexb; 
          } unicodebytes = unicodebytes + "\\u" + Hexb; 
 } System.out.println ("Unicodebytes is:" + unicodebytes);         return unicodebytes; 
         public static string Decodeunicode (final String datastr) {int start = 0; 
         int end = 0; 
         Final StringBuffer buffer = new StringBuffer (); 
           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); } Char letter = (char) integer.parseint (CHARSTR, 16); 
          16 binary parse shaping string. 
          Buffer.append (new Character (letter). ToString ()); 
         start = end; 
       return buffer.tostring (); } 
     }
public static string Decodeunicode (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 xxxx int value = 0;  
 
     for (int i = 0; i < 4; i++) {Achar = Thestring.charat (x + +); Switch (achar) {case ' 0 ': Case ' 1 ': Case ' 2 ': Case ' 3 ': Case ' 4 ': CA Se ' 5 ': Case ' 6 ': Case ' 7 ': Case ' 8 ': Case ' 9 ': value = (value << 4) + ACha  
      R-' 0 ';  
      Break  Case ' A ': Case ' B ': Case ' C ': Case ' d ': Case ' e ': Case ' f ': value = (value  
      << 4) + + Achar-' a ';  
      Break  
    Case ' A ': Case ' B ': Case ' C ': Case ' D ':  Case ' 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);  
 
    } else Outbuffer.append (Achar);  
 
   return outbuffer.tostring (); }

Above this Java Unicode and Chinese conversion of the simple implementation is small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.