java类型转换 byte转int

转换思路

1、存储大小:int是32位,byte是8位,所以要用4个byte转储

示例:
byte[0] 记录 8—1位;
byte[1] 记录 16—9位;
byte[2] 记录 24—17位;
byte[3] 记录 32—25位;

2、int直接强转为byte时,存储的是低8位。所以要完整记录32—1位,int类型的32位—9位之间需要进行移位,将其移至8—1位即可。

示例:
byte[0] 记录 8—1位 ——不移动——> b[0] = (byte) int;
byte[1] 记录 16—9位 ——右移8位——> b[1] = (byte) (int >> 8);
byte[2] 记录 24—17位 ——右移16位——> b[2] = (byte) (int >> 16);
byte[3] 记录 32—25位 ——右移24位——>b[3] = (byte) (int >> 24);

3、如果要将负数int转换为byte,必须要考虑:移位后的符号位问题。进行移位后,在int强转为byte之前如果int类型变量为负数,就需要将高位(符号位)进行&0xff处理。

示例:
byte[0] 记录 8—1位 ——不移动——> b[0] = (byte) int;
byte[1] 记录 16—9位 ——右移8位——> b[1] = (byte) (int >> 8 & 0xff);
byte[2] 记录 24—17位 ——右移16位——> b[2] = (byte) (int >> 16 & 0xff);
byte[3] 记录 32—25位 ——右移24位——>b[3] = (byte) (int >> 24 & 0xff);

代码演示

1、int转换为byte[],不考虑正负

    public static byte[] intToByteArray(int i){
        byte[] bytes = new byte[4];
        bytes[0] = (byte) i;
        bytes[1] = (byte)(i >> 8);
        bytes[2] = (byte)(i >> 16);
        bytes[3] = (byte)(i >> 24);
        return bytes;
    }
    
    public static int byteArrayToInt(byte[] bytes){
        int res = 0;
        for (int i = 0; i < bytes.length; i++) {
            res += (bytes[i] & 0xff) << i*8;
        }
        return res;
    }

2、int转换为byte[],不考虑正负,采用逆序转储

    public static byte[] intToByteArrayReverse(int i){
        byte[] bytes = new byte[4];
        b[0] = (byte)(i >> 24);
        b[1] = (byte)(i >> 16);
        b[2] = (byte)(i >> 8);
        b[3]= (byte) i;
        return bytes;
    }
    
    public static int byteArrayToIntReverse(byte[] bytes){
        int value=0;
        for(int i = 0; i < 4; i++) {
            value += (bytes[i] & 0xff) << (3-i) * 8;
        }
        return value;
    }

3、int转换为byte[],考虑到正负,加上 &0xff

    public static byte[] intToByteArrayIgnoreSign(int i){
        byte[] bytes = new byte[4];
        bytes[0] = (byte)(i & 0xff);
        bytes[1] = (byte)(i >> 8 & 0xff);
        bytes[2] = (byte)(i >> 16 & 0xff);
        bytes[3] = (byte)(i >> 24 & 0xff);
        return bytes;
    }
    
    //采用循环累加也行
  	public static int byteArrayToIntIgnoreSign(byte[] bytes){
        return (bytes[0] & 0xff)
                +((bytes[1] & 0xff) << 8)
                +((bytes[2] & 0xff) << 16)
                +((bytes[3] & 0xff) << 24);
    }

4、对intToByteArray()方法进行优化,用 >>> 代替 (>>、&0xff)

    public static byte[] intToByteArrayBest(int i){
         return new byte[]{
                (byte)i,
                (byte)(i >>> 8),
                (byte)(i >>> 16),
                (byte)(i >>> 24)};
    }
    
    //循环累加、或运算都可以
  	public static int byteArrayToIntBest(byte[] bytes){
        return (bytes[0] & 0xff)
                |((bytes[1] & 0xff) << 8)
                |((bytes[2] & 0xff) << 16)
                |((bytes[3] & 0xff) << 24);
    }

测试过程中发现的问题

1、在byte[]转换int类型时,代码没有写对,发现意外收获

    //错误写法:少写了 &0xff
    //发现在(0,127)之间结果正确
    public static int byteArrayToInt(byte[] bytes){
        int value=0;
        for(int i = 0; i < 4; i++) {
            value += bytes[i] << (3-i) * 8;
        }
        return value;
    }

    //错误写法:(bytes[i] & 0xff) 少了()
    //发现在(0,255)之间结果正确
    public static int byteArrayToInt(byte[] bytes){
        int value=0;
        for(int i = 0; i < 4; i++) {
            value += bytes[i] & 0xff << (3-i) * 8;
        }
        return value;
    }
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值