unicode与中文互换

001 package com.loyin.util;
002   
003 import java.lang.Character.UnicodeBlock;
004   
005 public class UnicodeTool {
006     /**
007       * 中文转换成 unicode
008       * @author fanhui
009       * 2007-3-15
010       * @param inStr
011       * @return
012       */
013      public static String encodeUnicode(String inStr) {
014             char[] myBuffer = inStr.toCharArray();
015              
016             StringBuffer sb = new StringBuffer();
017             for (int i = 0; i < inStr.length(); i++) {
018                 char ch=myBuffer[i];
019                 if((int)ch<10){
020                     sb.append("\\u000"+(int)ch);
021                     continue;
022                 }
023              UnicodeBlock ub = UnicodeBlock.of(ch);
024                 if(ub == UnicodeBlock.BASIC_LATIN){
025                  //英文及数字等
026                  sb.append(myBuffer[i]);
027                 }else if(ub == UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS){
028                  //全角半角字符
029                  int j = (int) myBuffer[i] - 65248;
030                  sb.append((char)j);
031                 }else{
032                  //汉字
033                  short s = (short) myBuffer[i];
034                     String hexS = Integer.toHexString(s);
035                     String unicode = "\\u"+hexS;
036                  sb.append(unicode.toLowerCase());
037                 }
038             }
039             return sb.toString();
040         }
041        
042      /**
043       * unicode 转换成 中文
044       * @author fanhui
045       * 2007-3-15
046       * @param theString
047       * @return
048       */
049      public static String decodeUnicode(String theString) {
050       char aChar;
051       int len = theString.length();
052       StringBuffer outBuffer = new StringBuffer(len);
053       for (int x = 0; x < len;) {
054        aChar = theString.charAt(x++);
055        if (aChar == '\\') {
056         aChar = theString.charAt(x++);
057         if (aChar == 'u') {
058          // Read the xxxx
059          int value = 0;
060          for (int i = 0; i < 4; i++) {
061           aChar = theString.charAt(x++);
062           switch (aChar) {
063           case '0':
064           case '1':
065           case '2':
066           case '3':
067           case '4':
068           case '5':
069           case '6':
070           case '7':
071           case '8':
072           case '9':
073            value = (value << 4) + aChar - '0';
074            break;
075           case 'a':
076           case 'b':
077           case 'c':
078           case 'd':
079           case 'e':
080           case 'f':
081            value = (value << 4) + 10 + aChar - 'a';
082            break;
083           case 'A':
084           case 'B':
085           case 'C':
086           case 'D':
087           case 'E':
088           case 'F':
089            value = (value << 4) + 10 + aChar - 'A';
090            break;
091           default:
092            throw new IllegalArgumentException(
093              "Malformed   \\uxxxx   encoding.");
094           }
095          }
096          outBuffer.append((char) value);
097         } else {
098          if (aChar == 't')
099           aChar = '\t';
100          else if (aChar == 'r')
101           aChar = '\r';
102          else if (aChar == 'n')
103           aChar = '\n';
104          else if (aChar == 'f')
105           aChar = '\f';
106          outBuffer.append(aChar);
107         }
108        } else
109         outBuffer.append(aChar);
110       }
111       return outBuffer.toString();
112      }
113   
114 }

你可能感兴趣的:(UNICODE与中文互换,unicode编解码)