public static byte[] hexStrToBinaryStr(String hexString) {
if (StringUtils.isEmpty(hexString)) {
return null;
}
hexString = hexString.replaceAll(" ", "");
int len = hexString.length();
int index = 0;
byte[] bytes = new byte[len / 2];
while (index < len) {
String sub = hexString.substring(index, index + 2);
bytes[index/2] = (byte)Integer.parseInt(sub,16);
index += 2;
}
return bytes;
}
字节数组转十六进制字符串
public static String BinaryToHexString(byte[] bytes) {
String hexStr = "0123456789ABCDEF";
String result = "";
String hex = "";
for (byte b : bytes) {
hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
hex += String.valueOf(hexStr.charAt(b & 0x0F));
result += hex + " ";
}
return result;
}
十六进制字符串转为字符串集合
public static ArrayList<String> toStringList(String hexData) {
ArrayList<String> arrayList = new ArrayList<>();
if (hexData == null || hexData.equals("")) {
return null;
}
hexData = hexData.replaceAll(" ", "");
int len = hexData.length();
if (len % 2 != 0) {
return null;
}
int num = 0;
while (num < len) {
String s = hexData.substring(num, num + 2);
arrayList.add(s);
num = num + 2;
}
return arrayList;
}
十六进制字符串转为十进制整形集合
public static ArrayList<Integer> toIntegerList(String hexData) {
ArrayList<Integer> arrayList = new ArrayList<>();
if (hexData == null || hexData.equals("")) {
return null;
}
hexData = hexData.replaceAll(" ", "");
int len = hexData.length();
if (len % 2 != 0) {
return null;
}
int num = 0;
while (num < len) {
String s = hexData.substring(num, num + 2);
arrayList.add(Integer.parseInt(s, 16));
num = num + 2;
}
return arrayList;
}