unicode编码和汉字字符串转互相转换

package com.zdyl.wxapplet.common.utils;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * unicode编码和汉字字符串转换工具
 */
public class UnicodeUtils {

    public static void main(String[] args) throws UnsupportedEncodingException {
        String s = "简介";
        System.out.println(s + " --的unicode编码是:" + gbEncoding(s));
        System.out.println(gbEncoding(s) + " --转换成中文是:" + unicodeToString(gbEncoding(s)));

        //System.out.println(gbEncoding(s) + " --转换成中文是:"+decodeUnicode("\\u7b80\\u4ecb"));
    }

    /*
     * 中文转unicode编码
     */
    public static String gbEncoding(final String gbString) {
        char[] utfBytes = gbString.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转 汉字字符串
     *
     * @param str \u6728
     * @return '木' 26408
     */
    public static String unicodeToString(String str) {

        Pattern pattern = Pattern.compile("(u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(str);
        char ch;
        while (matcher.find()) {
            //group 6728
            String group = matcher.group(2);
            //ch:'木' 26408
            ch = (char) Integer.parseInt(group, 16);
            //group1 \u6728
            String group1 = matcher.group(1);
            str = str.replace(group1, ch + "");
        }
        return str;
    }
}

 

posted @ 2020-11-27 16:58  william_zhao  阅读(535)  评论(0编辑  收藏  举报