摘要:
/** * 将16进制字符串转化成二进制字节数组 * @param hexString 字符串 * @return 数组 */private byte[] hexStringToB...
/**
* 将16进制字符串转化成二进制字节数组
* @param hexString 字符串
* @return 数组
*/
private byte[] hexStringToBinaryArray(String hexString) {
int len = hexString.length();
byte[] binaryArray = new byte[len * 4];
for (int i = 0; i < len; i++) {
char c = hexString.charAt(i);
int value = Character.digit(c, 16);
for (int j = 0; j < 4; j++) {
binaryArray[i * 4 + (3 - j)] = (byte) ((value >> j) & 0x01);
}
}
return binaryArray;
}
@Test
void contextLoads() {
String tempStr = "00000917";// 十六进制hex字符串
byte[] binaryArray = hexStringToBinaryArray(tempStr);
StringBuilder binaryString = new StringBuilder();
// 输出转换后的2进制数组
for (byte b : binaryArray) {
binaryString.append(b);
}
System.err.println(binaryString);// 经过转化后的二进制字符串
}