byte数组转16进制
代码语言:javascript复制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数组
代码语言:javascript复制/**
* 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进制
代码语言:javascript复制strSerial = bytes2hexStr(struOut.byData);
System.out.println(strSerial);
Long.parseLong(strSerial, 16)
System.out.println("读卡成功,内容为" Long.parseLong(strSerial, 16));