|  | @@ -0,0 +1,203 @@
 | 
	
		
			
				|  |  | +package com.example.utils;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +/**
 | 
	
		
			
				|  |  | + * @category 十六进制组件
 | 
	
		
			
				|  |  | + *
 | 
	
		
			
				|  |  | + * @author DSJ
 | 
	
		
			
				|  |  | + * @version 1.0
 | 
	
		
			
				|  |  | + * @since 1.0
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  | +public class Hex {
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 用于建立十六进制字符的输出的小写字符数组
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5',
 | 
	
		
			
				|  |  | +            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 用于建立十六进制字符的输出的大写字符数组
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    private static final char[] DIGITS_UPPER = {'0', '1', '2', '3', '4', '5',
 | 
	
		
			
				|  |  | +            '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 将字节数组转换为十六进制字符数组
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param data byte[]
 | 
	
		
			
				|  |  | +     * @return 十六进制char[]
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static char[] encodeHex(byte[] data) {
 | 
	
		
			
				|  |  | +        return encodeHex(data, true);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 将字节数组转换为十六进制字符数组
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param data        byte[]
 | 
	
		
			
				|  |  | +     * @param toLowerCase <code>true</code> 传换成小写格式 , <code>false</code> 传换成大写格式
 | 
	
		
			
				|  |  | +     * @return 十六进制char[]
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static char[] encodeHex(byte[] data, boolean toLowerCase) {
 | 
	
		
			
				|  |  | +        return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 将字节数组转换为十六进制字符数组
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param data     byte[]
 | 
	
		
			
				|  |  | +     * @param toDigits 用于控制输出的char[]
 | 
	
		
			
				|  |  | +     * @return 十六进制char[]
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    protected static char[] encodeHex(byte[] data, char[] toDigits) {
 | 
	
		
			
				|  |  | +        int l = data.length;
 | 
	
		
			
				|  |  | +        char[] out = new char[l << 1];
 | 
	
		
			
				|  |  | +        // two characters form the hex value.
 | 
	
		
			
				|  |  | +        for (int i = 0, j = 0; i < l; i++) {
 | 
	
		
			
				|  |  | +            out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
 | 
	
		
			
				|  |  | +            out[j++] = toDigits[0x0F & data[i]];
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return out;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 将字节数组转换为十六进制字符串
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param data byte[]
 | 
	
		
			
				|  |  | +     * @return 十六进制String
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static String encodeHexStr(byte[] data) {
 | 
	
		
			
				|  |  | +        return encodeHexStr(data, true);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public static String encodeHexString(byte[] data){
 | 
	
		
			
				|  |  | +        return encodeHexStr(data);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 将字节数组转换为十六进制字符串
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param data        byte[]
 | 
	
		
			
				|  |  | +     * @param toLowerCase <code>true</code> 传换成小写格式 , <code>false</code> 传换成大写格式
 | 
	
		
			
				|  |  | +     * @return 十六进制String
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static String encodeHexStr(byte[] data, boolean toLowerCase) {
 | 
	
		
			
				|  |  | +        return encodeHexStr(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 将字节数组转换为十六进制字符串
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param data     byte[]
 | 
	
		
			
				|  |  | +     * @param toDigits 用于控制输出的char[]
 | 
	
		
			
				|  |  | +     * @return 十六进制String
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    protected static String encodeHexStr(byte[] data, char[] toDigits) {
 | 
	
		
			
				|  |  | +        return new String(encodeHex(data, toDigits));
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 将十六进制字符数组转换为字节数组
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param data 十六进制char[]
 | 
	
		
			
				|  |  | +     * @return byte[]
 | 
	
		
			
				|  |  | +     * @throws RuntimeException 如果源十六进制字符数组是一个奇怪的长度,将抛出运行时异常
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static byte[] decodeHex(char[] data) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        int len = data.length;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if ((len & 0x01) != 0) {
 | 
	
		
			
				|  |  | +            throw new RuntimeException("Odd number of characters.");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        byte[] out = new byte[len >> 1];
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // two characters form the hex value.
 | 
	
		
			
				|  |  | +        for (int i = 0, j = 0; j < len; i++) {
 | 
	
		
			
				|  |  | +            int f = toDigit(data[j], j) << 4;
 | 
	
		
			
				|  |  | +            j++;
 | 
	
		
			
				|  |  | +            f = f | toDigit(data[j], j);
 | 
	
		
			
				|  |  | +            j++;
 | 
	
		
			
				|  |  | +            out[i] = (byte) (f & 0xFF);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return out;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**将二进制转换成16进制
 | 
	
		
			
				|  |  | +     * @param buf
 | 
	
		
			
				|  |  | +     * @return
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static String parseByte2HexStr(byte buf[]) {
 | 
	
		
			
				|  |  | +        StringBuffer sb = new StringBuffer();
 | 
	
		
			
				|  |  | +        for (int i = 0; i < buf.length; i++) {
 | 
	
		
			
				|  |  | +            String hex = Integer.toHexString(buf[i] & 0xFF);
 | 
	
		
			
				|  |  | +            if (hex.length() == 1) {
 | 
	
		
			
				|  |  | +                hex = '0' + hex;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            sb.append(hex.toUpperCase());
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return sb.toString();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**将16进制转换为二进制
 | 
	
		
			
				|  |  | +     * @param hexStr
 | 
	
		
			
				|  |  | +     * @return
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static byte[] parseHexStr2Byte(String hexStr) {
 | 
	
		
			
				|  |  | +        if (hexStr.length() < 1)
 | 
	
		
			
				|  |  | +            return null;
 | 
	
		
			
				|  |  | +        byte[] result = new byte[hexStr.length()/2];
 | 
	
		
			
				|  |  | +        for (int i = 0;i< hexStr.length()/2; i++) {
 | 
	
		
			
				|  |  | +            int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
 | 
	
		
			
				|  |  | +            int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
 | 
	
		
			
				|  |  | +            result[i] = (byte) (high * 16 + low);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return result;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 将十六进制字符转换成一个整数
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param ch    十六进制char
 | 
	
		
			
				|  |  | +     * @param index 十六进制字符在字符数组中的位置
 | 
	
		
			
				|  |  | +     * @return 一个整数
 | 
	
		
			
				|  |  | +     * @throws RuntimeException 当ch不是一个合法的十六进制字符时,抛出运行时异常
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    protected static int toDigit(char ch, int index) {
 | 
	
		
			
				|  |  | +        int digit = Character.digit(ch, 16);
 | 
	
		
			
				|  |  | +        if (digit == -1) {
 | 
	
		
			
				|  |  | +            throw new RuntimeException("Illegal hexadecimal character " + ch
 | 
	
		
			
				|  |  | +                    + " at index " + index);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return digit;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +//    public static void main(String[] args) {
 | 
	
		
			
				|  |  | +//        String srcStr = "qwer";
 | 
	
		
			
				|  |  | +//        String encodeStr = encodeHexStr(srcStr.getBytes());
 | 
	
		
			
				|  |  | +//        String decodeStr = new String(decodeHex(encodeStr.toCharArray()));
 | 
	
		
			
				|  |  | +//        System.out.println("before encode:" + srcStr);
 | 
	
		
			
				|  |  | +//        System.out.println("after encode:" + encodeStr);
 | 
	
		
			
				|  |  | +//        System.out.println("convert:" + decodeStr);
 | 
	
		
			
				|  |  | +//
 | 
	
		
			
				|  |  | +//        test();
 | 
	
		
			
				|  |  | +//    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public static void  test() {
 | 
	
		
			
				|  |  | +        String hello = "asdfasdf你还要asdfasdfasdfasdfl";
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            String hexString = Hex.encodeHexString(hello.getBytes("utf-8"));
 | 
	
		
			
				|  |  | +            String hexString2 = Hex.encodeHexString(hello.getBytes("ascii"));
 | 
	
		
			
				|  |  | +            String hexString3 = Hex.encodeHexString(hello.getBytes("gbk"));
 | 
	
		
			
				|  |  | +            String hexString4 = new String(Hex.encodeHex(hello.getBytes("utf-8"), false));
 | 
	
		
			
				|  |  | +            System.out.println(hexString);
 | 
	
		
			
				|  |  | +            System.out.println(hexString2);
 | 
	
		
			
				|  |  | +            System.out.println(hexString3);
 | 
	
		
			
				|  |  | +            System.out.println(hexString4);
 | 
	
		
			
				|  |  | +        } catch (Exception e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 |