java实现unicode和中文互相转换

一、中文转Unicode

/**
     * @Title: unicodeEncode 
     * @Description: unicode编码
     * @param string
     * @return
     */
    public static String unicodeEncode(String string) {
        char[] utfBytes = string.toCharArray();
        String unicodeBytes = "";
        for (int i = 0; i < utfBytes.length; i++) {
            String hexB = Integer.toHexString(utfBytes[i]);
            if (hexB.length() <= 2) {
                hexB = "00" + hexB;
            }
            unicodeBytes = unicodeBytes + "\\u" + hexB;
        }
        return unicodeBytes;
    }

二、Unicode转中文

/**
     * @Title: unicodeDecode 
     * @Description: unicode解码
     * @param str
     * @return
     */
    public static String unicodeDecode(String string) {
        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(string);
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            string = string.replace(matcher.group(1), ch + "");
        }
        return string;
    }

三、测试代码:

public static void main(String[] args) {
        String sourceData = "这是原始的数据!!!";
        String unicodeEncode = unicodeEncode(sourceData);
        System.out.println("编码结果:");
        System.out.println(unicodeEncode);//\u8fd9\u662f\u539f\u59cb\u7684\u6570\u636e\uff01\uff01\uff01

        String unicodeDecode = unicodeDecode(unicodeEncode);
        System.out.println("解码结果:");
        System.out.println(unicodeDecode);//这是原始的数据!!!
    }

你可能感兴趣的:(JAVAEE)