Android学习心得(19) --- short、byte、Hex、int、ULeb128类型转化Java源码

这篇博客分享了作者在学习Android过程中遇到的类型转换问题,详细介绍了如何将byte数组转换为int、short,以及如何进行Hex转换。包括byte2int、byte2short、bytesToHexString等方法的Java源码实现,同时讲解了readUnsignedLeb128和decodeUleb128这两个与dex文件解析相关的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我在博客上发表一些我的Android学习心得,希望对大家能有帮助。
这一篇我们讲述一下在分析dex时候遇到类型转换,并附上java源码


1、介绍

由于读取dex文件放入的是一个byte数组,当我们对这个byte数组进行解析的时候,肯定会遇到一些类型转化的问题,下面就通过java源代码来解释一下这些类型转换 。

2、byte2int

byte[4]数组转化为int类型:

// 字节转换为十进制
    public static int byte2int(byte[] res) {
        int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00) | ((res[2] << 24) >>> 8) | (res[3] << 24);
        return targets;
    }

3、byte2short

// 字节类型转化为短整型
    public static short byte2Short(byte[] b) {
        short s = 0;
        short s0 = (short) (b[0] & 0xff);
        short s1 = (short) (b[1] & 0xff);
        s1 <<= 8;
        s = (short) (s0 | s1);
        return s;
    }

4、bytesToHexString

// 字节转化为十六进制字符串
    public static String bytesToHexString(byte[] src) {
        // byte[] src = reverseBytes(src1);
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            // 变成正数
            int v = src[i] & 0xFF;
            // Integer.toHexString 转化成十六进制
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv + " ");
        }
        return stringBuilder.toString();
    }

5、short2Byte

// 短整型转化为字符类型
    public static byte[] short2Byte(short number) {
        int temp = number;
        byte[] b = new byte[2];
        for (int i = 0; i < b.length; i++) {
            b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
            temp = temp >> 8; // 向右移8位
        }
        return b;
    }

6、int2Byte

// 十进制转化为字节
    public static byte[] int2Byte(final int integer) {
        int byteNum = (40 - Integer.numberOfLeadingZeros(integer < 0 ? ~integer : integer)) / 8;
        byte[] byteArray = new byte[4];

        for (int n = 0; n < byteNum; n++)
            byteArray[3 - n] = (byte) (integer >>> (n * 8));

        return (byteArray);
    }

7、readUnsignedLeb128

// 读取uleb类型
    public static byte[] readUnsignedLeb128(byte[] srcByte, int offset) {
        List<Byte> byteAryList = new ArrayList<Byte>();
        byte bytes = Util.copyByte(srcByte, offset, 1)[0];
        byte highBit = (byte) (bytes & 0x80);
        byteAryList.add(bytes);
        offset++;
        while (highBit != 0) {
            bytes = Util.copyByte(srcByte, offset, 1)[0];
            highBit = (byte) (bytes & 0x80);
            offset++;
            byteAryList.add(bytes);
        }
        byte[] byteAry = new byte[byteAryList.size()];
        for (int j = 0; j < byteAryList.size(); j++) {
            byteAry[j] = byteAryList.get(j);
        }
        return byteAry;
    }

8、decodeUleb128

// 解码uleb
    public static int decodeUleb128(byte[] byteAry) {
        int index = 0, cur;
        int result = byteAry[index];
        index++;

        if (byteAry.length == 1) {
            return result;
        }

        if (byteAry.length == 2) {
            cur = byteAry[index];
            index++;
            result = (result & 0x7f) | ((cur & 0x7f) << 7);
            return result;
        }

        if (byteAry.length == 3) {
            cur = byteAry[index];
            index++;
            result |= (cur & 0x7f) << 14;
            return result;
        }

        if (byteAry.length == 4) {
            cur = byteAry[index];
            index++;
            result |= (cur & 0x7f) << 21;
            return result;
        }

        if (byteAry.length == 5) {
            cur = byteAry[index];
            index++;
            result |= cur << 28;
            return result;
        }

        return result;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值