查看我的博客:https://www.zjhuiwan.cn
byte数组转16进制
private static final char[] HEX_CHARS = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
/*
* byte[]数组转十六进制
*/
public static String bytes2hexStr(byte[] bytes) {
int len = bytes.length;
if (len == 0) {
return null;
}
char[] cbuf = new char[len * 2];
for (int i = 0; i < len; i++) {
int x = i * 2;
cbuf[x] = HEX_CHARS[(bytes[i] >>> 4) & 0xf];
cbuf[x + 1] = HEX_CHARS[bytes[i] & 0xf];
}
return new String(cbuf);
}
16进制转byte数组
/**
* hex字符串转byte数组
*
* @param inHex 待转换的Hex字符串
* @return 转换后的byte数组结果
*/
public static byte[] hexToByteArray(String inHex) {
int hexlen = inHex.length();
byte[] result;
if (hexlen % 2 == 1) {
// 奇数
hexlen++;
result = new byte[(hexlen / 2)];
inHex = "0" + inHex;
} else {
// 偶数
result = new byte[(hexlen / 2)];
}
int j = 0;
for (int i = 0; i < hexlen; i += 2) {
result[j] = hexToByte(inHex.substring(i, i + 2));
j++;
}
return result;
}
16进制转10进制
strSerial = bytes2hexStr(struOut.byData);
System.out.println(strSerial);
makeDevice(lUserID, "2", "1", "3", "2");
System.out.println("读卡成功,内容为" + Long.parseLong(strSerial, 16));
16进制补足32位
while (cardNo.length() < 32) {
cardNo = "0" + cardNo;
}
if (cardNo.length() > 32) {
cardNo = cardNo.substring(0, 16);
}