博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java十六进制和byte数组转换
阅读量:4156 次
发布时间:2019-05-25

本文共 1460 字,大约阅读时间需要 4 分钟。

查看我的博客: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);}

转载地址:http://fqwxi.baihongyu.com/

你可能感兴趣的文章
求二叉树中结点的最大值(所有结点的值都是正整数)
查看>>
用go的flag包来解析命令行参数
查看>>
来玩下go的http get
查看>>
队列和栈的本质区别
查看>>
matlab中inline的用法
查看>>
如何用matlab求函数的最值?
查看>>
Git从入门到放弃
查看>>
java8采用stream对集合的常用操作
查看>>
EasySwift/YXJOnePixelLine 极其方便的画出真正的一个像素的线
查看>>
Ubuntu系统上安装Nginx服务器的简单方法
查看>>
Ubuntu Linux系统下apt-get命令详解
查看>>
ubuntu 16.04 下重置 MySQL 5.7 的密码(忘记密码)
查看>>
Ubuntu Navicat for MySQL安装以及破解方案
查看>>
HTTPS那些事 用java实现HTTPS工作原理
查看>>
oracle函数trunc的使用
查看>>
MySQL 存储过程或者函数中传参数实现where id in(1,2,3,...)IN条件拼接
查看>>
java反编译
查看>>
Class.forName( )你搞懂了吗?——转
查看>>
jarFile
查看>>
EJB与JAVA BEAN_J2EE的异步消息机制
查看>>