1. 程式人生 > >java 16進位制字串轉二進位制(byte[])

java 16進位制字串轉二進位制(byte[])

public static byte[] hexStr2Byte(String hex) {
    ByteBuffer bf = ByteBuffer.allocate(hex.length() / 2);
    for (int i = 0; i < hex.length(); i++) {
        String hexStr = hex.charAt(i) + "";
        i++;
        hexStr += hex.charAt(i);
        byte b = (byte) Integer.parseInt(hexStr, 16);
        bf.put(b);
    }
    return 
bf.array(); }