栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java类型转换 byte转int

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/976129.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号