生活不易、且行且珍惜。网站首页 程序人生
Java十六进制和byte数组转换
发布时间:2020-11-18 14:20编辑:zj 阅读:846文章分类:Java互动QQ群:170915747
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);
}/**
* 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);
Long.parseLong(strSerial, 16)
System.out.println("读卡成功,内容为" + Long.parseLong(strSerial, 16));#去评论一下
标签:#工具类
版权声明:本博客的所有原创内容皆为作品作者所有
转载请注明:来自ZJBLOG 链接:www.zjhuiwan.cn
24 +1
「万物皆有时,比如你我相遇」
感谢大佬打赏【请选择支付宝或微信,再选择金额】
使用微信扫描二维码完成支付
上一篇:蓝天白云


![[冒泡专用表情]](https://www.zjhuiwan.cn/images/images/zjemogi.png)
![[哈哈]](https://www.zjhuiwan.cn/images/images/laugh.gif)
![[衰]](https://www.zjhuiwan.cn/images/images/cry.gif)
![[伤心]](https://www.zjhuiwan.cn/images/images/unheart.gif)
![[猪头]](https://www.zjhuiwan.cn/images/images/pig.gif)
![[蛋糕]](https://www.zjhuiwan.cn/images/images/cake.gif)