`
yangmeng_3331
  • 浏览: 88079 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

java字符串与unicode转换

    博客分类:
  • Java
阅读更多
    /** 
     * 字符串转化为unicode 
     * @param gbString 
     * @return 
     */ 
    public static String encodeUnicode(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; 
            unicodeBytes = unicodeBytes + hexB; 
        } 
        System.out.println(unicodeBytes); 
        return unicodeBytes; 
    } 

    // unicode转化汉字 
    public static StringBuffer decodeUnicode(String utfStr) { 
        final StringBuffer buffer = new StringBuffer(); 
        String charStr = ""; 
        String operStr = utfStr; 
        for(int i =0 ; i < utfStr.length() ;i=+4){ 
            charStr = operStr.substring(0, 4); 
            operStr = operStr.substring(4, operStr.length()); 
            char letter = (char) Integer.parseInt(charStr, 16); 
            buffer.append(new Character(letter).toString()); 
        } 
        return buffer; 
    }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics