中文转成Unicode编码

筅森魡賤 提交于 2020-02-24 20:04:12
public class Main {
    public static void main(String[] args) {
        String uname = "编码";
        for (int i = 0; i < uname.length(); i++) {
            char unamechar = uname.charAt(i);
System.out.println(unamechar + "=" + gbEncoding(String.valueOf(unamechar)));
}


        String ucode = "\u7f16\u7801";
System.out.println(ucode);
}

    /**
     * 把中文转成Unicode编码
     *
     * @param gbString
* @return
*/
private static String gbEncoding(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;
}
        return unicodeBytes;
}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!