`
yeak2001
  • 浏览: 101085 次
  • 性别: Icon_minigender_1
  • 来自: 无锡
社区版块
存档分类
最新评论

Java如何把javascript格式的hex转换成Java格式的hex

阅读更多
首先由于正常的ASCII码表示的字符有限,于是产生了Unicode, Unicode使用的是16进制的格式来表示一个字符.
在javascript里Unicode的表示格式有2种: 1. '%uxxxx' 2. '\uxxxx'.
在Java里Unicode格式就一种'\u'.
Javascript函数escape转义的字符使用的就是'%u',于是这个hex到了java里就认不出来了,就会出现乱码.

Solution:

     1. 不管是'%u'或者'\u',他们都是hex在某种语言里的表示格式,真正的hex number还是在后面的数字. 所以只要把数字取出来加以处理就好了.

     2. 数字取出来以后是16进制的,char和Integer 之间是可以隐式转换的,我们现在要做的就是把16进制的数字转换成10进制的Integer,然后转换成char,对应的字符就出来了.

/**
        * Decode given string.
        * Java just know the hex form '\u2122', not know how to translate other form hex.
        * If string include the javascript hex, like '%u2122'. This method will get the real hex number and cast it to right format char.
        * @param input_str the string to decode
        * @return an decode string
        */
        public static String decodeFromHex(String input_str) {
              Pattern p = Pattern. compile( "%u([a-zA-Z0-9]{4})");
              Matcher m = p.matcher(input_str);
              StringBuffer sb = new StringBuffer();
               while (m.find()) {
                m.appendReplacement(sb,
                    String. valueOf(( char) Integer. parseInt(m.group(1), 16)));
              }
              m.appendTail(sb);
               return sb.toString();
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics