unicode编码转汉字

public static void main(String[] args) throws UnsupportedEncodingException  {
 int y = 20892;
decodeUnicode("U+"+Integer.toHexString(y))
}
 
//中文转Unicode  
      public static String gbEncoding(final String gbString) {   //gbString = "测试" 
    char[] utfBytes = gbString.toCharArray();   //utfBytes = [测, 试]  
           String unicodeBytes = "";    
    for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {     

            String hexB = Integer.toHexString(utfBytes[byteIndex]);   //转换为16进制整型字符串 

    if (hexB.length() <= 2) {     

                hexB = "00" + hexB;     

  1.                }     
  2.                unicodeBytes = unicodeBytes + "\u" + hexB;     
  3.           }     
  4.           System.out.println("unicodeBytes is: " + unicodeBytes);     
  5. return unicodeBytes;     
  6.       }  


 
    1. //Unicode转中文  
    2.       public static String decodeUnicode(final String dataStr) {     
    3.          int start = 0;     
    4.          int end = 0;     
    5.          final StringBuffer buffer = new StringBuffer();     
    6.          while (start > -1) {     
    7.              end = dataStr.indexOf("\u", start + 2);     
    8.              String charStr = "";     
    9.              if (end == -1) {     
    10.                  charStr = dataStr.substring(start + 2, dataStr.length());     
    11.              } else {     
    12.                  charStr = dataStr.substring(start + 2, end);     
    13.              }     
    14.              char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。     
    15.              buffer.append(new Character(letter).toString());     
    16.              start = end;     
    17.          }     
    18.          return buffer.toString();     
    19.       } 
原文地址:https://www.cnblogs.com/kongxc/p/6344554.html