Java版本bin2hex 函数,对应PHP的bin2hex

public class Bin2Hex {

    /**
     * 字符串转换成十六进制值
     * @param bin         
     * @return String 我们看到的要转换成十六进制的字符串
     */
    public static String bin2hex(String bin) {
        char[] digital = "0123456789ABCDEF".toCharArray();
        StringBuffer sb = new StringBuffer("");
        byte[] bs = bin.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(digital[bit]);
            bit = bs[i] & 0x0f;
            sb.append(digital[bit]);
        }
        return sb.toString();
    }

    /**
     * 十六进制转换字符串
     * @param hex  String 十六进制       
     * @return String 转换后的字符串
     */
    public static String hex2bin(String hex) {
        String digital = "0123456789ABCDEF";
        char[] hex2char = hex.toCharArray();
        byte[] bytes = new byte[hex.length() / 2];
        int temp;
        for (int i = 0; i < bytes.length; i++) {
            temp = digital.indexOf(hex2char[2 * i]) * 16;
            temp += digital.indexOf(hex2char[2 * i + 1]);
            bytes[i] = (byte) (temp & 0xff);
        }
        return new String(bytes);
    }

    /**
     * java字节码转字符串
     * 
     * @param b
     * @return
     */
    public static String byte2hex(byte[] b) { // 一个字节的数,
        // 转成16进制字符串
        String hs = "";
        String tmp = "";
        for (int n = 0; n < b.length; n++) {
            // 整数转成十六进制表示

            tmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (tmp.length() == 1) {
                hs = hs + "0" + tmp;
            } else {
                hs = hs + tmp;
            }
        }
        tmp = null;
        return hs.toUpperCase(); // 转成大写
    }

    /**
     * 字符串转java字节码
     * @param b
     * @return
     */
    public static byte[] hex2byte(byte[] b) {
        if ((b.length % 2) != 0) {
            throw new IllegalArgumentException("长度不是偶数");
        }
        byte[] b2 = new byte[b.length / 2];
        for (int n = 0; n < b.length; n += 2) {
            String item = new String(b, n, 2);
            // 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个进制字节

            b2[n / 2] = (byte) Integer.parseInt(item, 16);
        }
        b = null;
        return b2;
    }

    public static void main(String[] args) {
        // String content = "AlanChen";
        String content = "{\"mobile\":\"jEz7xHvZfriV8xFdvgDFhA==\",\"tid\":50,\"tuid\":\"17688733833\",\"timestamp\":1565358767,\"token\":\"049D9333527C0CE6FB15ED69567146D3\"}";

        String hexString = bin2hex(content);
        System.out.println(hexString);
        System.out.println(hex2bin(hexString));
    }
}

你可能感兴趣的:(Java版本bin2hex 函数,对应PHP的bin2hex)