字节转为16进制输出

#字节转为16进制输出

字节码文件通过hexadecimal格式打开时,显示的为16进制的字符表示
每一个16进制串代表连个字节例如-84 -19两个字节表示为aced

    private static int readTimes = 1;
    public static void readByte2Hex(byte[] bs) {
        byte[] buffer = new byte[2];
        int tmp1, tmp2, tmp3;
        for (int i = 0; i < bs.length; i++) {
            if (i % 2 == 0) {
                buffer[0] = bs[i];
            }
            if (i % 2 == 1) {
                buffer[1] = bs[i];
                tmp1 = (int) buffer[0];
                tmp1 = tmp1 << 8;
                tmp1 = tmp1 & 0x0000FF00;
                tmp2 = (int) buffer[1];
                tmp2 = tmp2 & 0x000000FF; // 255
                tmp3 = tmp1 | tmp2;
                sysOutBytes(tmp3);
                readTimes++;
            }
        }
    }
    private static void sysOutBytes(int tmp3) {
        System.out.println(tmp3);
        String hex = Integer.toHexString(tmp3);
        while (hex.length() < 4) {
            hex = "0" + hex;
        }
        System.out.print(hex + " ");
        if (readTimes % 8 == 0) {
            System.out.println();
        }
    }

转载于:https://my.oschina.net/sandant/blog/698877

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值