欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java中unicode和中文相互转换的简单实现

程序员文章站 2024-03-13 12:10:09
如下所示: package test.com.gjob.services; import java.util.properties; public...

如下所示:

package test.com.gjob.services;
  import java.util.properties;
   public class test { 
      public static void main(string[] args) { 
      string s = "简介"; 
      string tt = gbencoding(s); 
  //    string tt1 = "你好,我想给你说一个事情";
      system.out.println(decodeunicode("\\u7b80\\u4ecb")); 
  //    system.out.println(decodeunicode(tt1)); 
      system.out.println(htmldecoder.decode("中国"));
      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(utfbytes[byteindex]); 
            if (hexb.length() <= 2) { 
              hexb = "00" + 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进制parse整形字符串。 
          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':  
 
     case '5':  
 
     case '6':  
      case '7':  
      case '8':  
      case '9':  
      value = (value << 4) + achar - '0';  
      break;  
      case 'a':  
      case 'b':  
      case 'c':  
      case 'd':  
      case 'e':  
      case 'f':  
      value = (value << 4) + 10 + achar - 'a';  
      break;  
      case 'a':  
      case 'b':  
      case 'c':  
      case 'd':  
      case 'e':  
      case 'f':  
      value = (value << 4) + 10 + 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();  
 
   }

以上这篇java中unicode和中文相互转换的简单实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。