Java实现byte数组与Hex互转

十六进制字符的输出大写字符:0123456789ABCDEF
十六进制字符的输出小写字符:0123456789abcdef

下面使用十六进制大写字符。

1、方式1

public class HexStringUtils {
    private static final char[] HEX_CHAR_TABLE = {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
    };
    private static final Map<Character, Byte> HEX_BYTE_MAP = new HashMap<>();

    static {
        for (int i = 0; i < HEX_CHAR_TABLE.length; i++) {
            char c = HEX_CHAR_TABLE[i];
            HEX_BYTE_MAP.put(c, (byte) i);
        }
    }

    /**
     * 将byte数组转换为表示16进制值的字符串
     *
     * @param byteArray 需要转换的byte数组
     * @return 转换后的字符串(16进制值)
     */
    public static String byteArrayToHexString(byte[] byteArray) {
        if (byteArray == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (byte b : byteArray) {
            sb.append(HEX_CHAR_TABLE[(b & 0xf0) >> 4]);
            sb.append(HEX_CHAR_TABLE[b & 0x0f]);
        }
        return sb.toString();
    }

    /**
     * 将表示16进制值的字符串转换为byte数组
     *
     * @param hexString 需要转换的字符串(16进制值)
     * @return 转换后的byte数组
     */
    public static byte[] hexStringToByteArray(String hexString) {
        if (StringUtils.isBlank(hexString)) {
            return null;
        }
        // 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
        byte[] byteArray = new byte[hexString.length() / 2];
        for (int i = 0; i < hexString.length() / 2; i++) {
            char hi = hexString.charAt(i * 2);
            char lo = hexString.charAt(i * 2 + 1);
            byteArray[i] = (byte) ((HEX_BYTE_MAP.get(hi) << 4) + HEX_BYTE_MAP.get(lo));
        }
        return byteArray;
    }

    public static void main(String[] args) {
        String str = "你好 asa;==ff--00=-=";
        //
        String hexString = byteArrayToHexString(str.getBytes());
        System.out.println(str.length());
        System.out.println(hexString);
        System.out.println(hexString.length());

        System.out.println("===========");
        byte[] bytes = hexStringToByteArray(hexString);
        System.out.println(Arrays.toString(bytes));
        System.out.println(new String(bytes));
    }
}

Java实现byte数组与Hex互转_第1张图片

2、方式2

public class HexStringUtils {

    public static byte[] hexStringToByteArray(String hexString) {
        if (StringUtils.isEmpty(hexString)) {
            return null;
        }
        hexString = hexString.toLowerCase();
        final byte[] byteArray = new byte[hexString.length() >> 1];
        int index = 0;
        for (int i = 0; i < hexString.length(); i++) {
            if (index > hexString.length() - 1) {
                return byteArray;
            }
            byte highDit = (byte) (Character.digit(hexString.charAt(index), 16) & 0xFF);
            byte lowDit = (byte) (Character.digit(hexString.charAt(index + 1), 16) & 0xFF);
            byteArray[i] = (byte) (highDit << 4 | lowDit);
            index += 2;
        }
        return byteArray;
    }


    public static String byteArrayToHexString(byte[] byteArray) {
        if (byteArray == null) {
            return null;
        }
        // HEX_CHAR_TABLE  0123456789abcdef
        char[] hexArray = "0123456789ABCDEF".toCharArray();
        char[] hexChars = new char[byteArray.length * 2];
        for (int j = 0; j < byteArray.length; j++) {
            int v = byteArray[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    public static void main(String[] args) {
        String str = "你好 asa;==ff--00=-=";
        //
        String hexString = byteArrayToHexString(str.getBytes());
        System.out.println(str.length());
        System.out.println(hexString);
        System.out.println(hexString.length());

        System.out.println("===========");
        byte[] bytes = hexStringToByteArray(hexString);
        System.out.println(Arrays.toString(bytes));
        System.out.println(new String(bytes));
    }

}

– 求知若饥,虚心若愚。

你可能感兴趣的:(Common,byte数组与Hex互转)