把中文转成Unicode码,并且以高低位展示

public class FontUtil {

    public static void main(String[] args) {
//        System.out.println(chinaToUnicode("未登陆!"));
//        System.out.println(decodeUnicode("u672au767bu9646uff01"));
        Double longi = 33.14898745000000000000;
        System.out.println(DDtoDMS(longi));
    }

    private static final byte[] bswap32(int x) {
        return new byte[] { (byte) (((x << 24) & 0xff000000) >> 24), (byte) (((x << 8) & 0x00ff0000) >> 16),
                (byte) (((x >> 8) & 0x0000ff00) >> 8), (byte) (((x >> 24) & 0x000000ff)) };
    }

    public static int bytesToInt(byte[] data) {
        int x = ((data[0] & 0xff) << 24) | ((data[1] & 0xff) << 16) | ((data[2] & 0xff) << 8) | (data[3] & 0xff);
        return x;
    }

    /**
     * 把中文转成Unicode码
     * 并且高低位展示
     * @param str
     * @return
     */
    public static String chinaHighToUnicode(String str) {
        String result = "";
        for (int i = 0; i < str.length(); i++) {
            int chr1 = (char) str.charAt(i);

            byte[] data = bswap32(chr1);
            chr1 = bytesToInt(data);
            String chars = Integer.toHexString(chr1);
//            result += "\u" + chars.substring(0, chars.length() - 4);
            chars = chars.substring(0, chars.length() - 4);
            if (chars.length() < 4) {
                chars = "0" + chars;
            }
            result += chars;

        }
        return result;
    }

    /**
     * 把中文转成Unicode码
     * 
     * @param str
     * @return
     */
    public static String chinaToUnicode(String str) {
        String result = "";
        for (int i = 0; i < str.length(); i++) {
            int chr1 = (char) str.charAt(i);
            if (chr1 >= 19968 && chr1 <= 171941) {// 汉字范围 u4e00-u9fa5 (中文)
                result += "\u" + Integer.toHexString(chr1);
            } else {
                result += str.charAt(i);
            }
        }
        return result;
    }

    /**
     * 判断是否为中文字符
     * 
     * @param c
     * @return
     */
    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }

    // Unicode转中文
    public static String decodeUnicode(final String unicode) {
        StringBuffer string = new StringBuffer();

        String[] hex = unicode.split("\\u");

        for (int i = 0; i < hex.length; i++) {

            try {
                // 汉字范围 u4e00-u9fa5 (中文)
                if (hex[i].length() >= 4) {// 取前四个,判断是否是汉字
                    String chinese = hex[i].substring(0, 4);
                    try {
                        int chr = Integer.parseInt(chinese, 16);
                        boolean isChinese = isChinese((char) chr);
                        // 转化成功,判断是否在 汉字范围内
                        if (isChinese) {// 在汉字范围内
                            // 追加成string
                            string.append((char) chr);
                            // 并且追加 后面的字符
                            String behindString = hex[i].substring(4);
                            string.append(behindString);
                        } else {
                            string.append(hex[i]);
                        }
                    } catch (NumberFormatException e1) {
                        string.append(hex[i]);
                    }

                } else {
                    string.append(hex[i]);
                }
            } catch (NumberFormatException e) {
                string.append(hex[i]);
            }
        }

        return string.toString();
    }

    /**
     * @Title: DDtoDMS
     * @Description: 度转换为经纬度
     * @Author youli
     * @date 2021年1月23日
     * @param d
     * @return
     */
    public static String DDtoDMS(Double d) {
        String[] array = d.toString().split("[.]");
        String degrees = array[0];// 得到度

        Double m = Double.parseDouble("0." + array[1]) * 60;
        String[] array1 = m.toString().split("[.]");
        String minutes = array1[0];// 得到分

        Double s = Double.parseDouble("0." + array1[1]) * 60;
        String[] array2 = s.toString().split("[.]");
        String seconds = array2[0];// 得到秒
        return degrees + "º" + minutes + "." + seconds + "’";
    }

}
原文地址:https://www.cnblogs.com/haoliyou/p/14357008.html