将十六进制转换为二进制
public static byte[] parseHexStr2Byte(String str){
if(str.length() < 1)
return null;
byte[] result = new byte[str.length()/2];
for (int i = 0;i< str.length()/2; i++) {
int high = Integer.parseInt(str.substring(i*2, i*2+1), 16);
int low = Integer.parseInt(str.substring(i*2+1, i*2+2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
将二进制转换为十六进制
public static String parseByte2HexStr(byte binary[]){
StringBuffer sb = new StringBuffer();
for(int i = 0; i < binary.length; i++){
String hex = Integer.toHexString(binary[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}