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

java byte数组与int之间相互转换

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

java byte数组与int之间相互转换

位运算
位运算说明使用
&按位与对应位都是1,结果为1,否则为0a&b
|按位或对应位都是0,结果为0,否则为1a|b
^按位非对应位值相同,结果为0,否则为1a^b
~按位取反每一位变相反位,即0变成1,1变成0~a
<<按位左移低位补0a<<2
>>按位右移保留符号位,0为正,1为负a>>2
>>>按位或高位补0a>>>2

补充:

  • 微机原理中,算数移动会保留符号位,逻辑移动不会保留符号位
  • byte数组》整型:利用byte,&,0xFF,|和<<
  • 整型》byte数组:利用value,&,0xFF,强制装换(byte),和>>
端序

端序:字节序

  • byte数据顺序与内存地址高低的关系
  • 小端:将byte数组的低byte放在内存的低地址处
  • 大端:将byte数组的低byte放在内存的高地址处

小端:一般都是小端

  • 低字节在后,byte数组中序号小的在右边

大端:

  • 高字节在后,byte数组中序号大的在右边
bytes》int
    
    public static int bytesToIntLittleEndian(byte[] bytes) {
        // byte数组中序号小的在右边
        return bytes[0] & 0xFF | //
                (bytes[1] & 0xFF) << 8 | //
                (bytes[2] & 0xFF) << 16 | //
                (bytes[3] & 0xFF) << 24; //
    }
    
    public static int bytesToIntBigEndian(byte[] bytes) {
        // byte数组中序号大的在右边
        return bytes[3] & 0xFF | //
                (bytes[2] & 0xFF) << 8 | //
                (bytes[1] & 0xFF) << 16 | //
                (bytes[0] & 0xFF) << 24; //
    }
int》bytes
    
    public static byte[] intToBytesLittleEndian(short intValue) {
        // byte数组中序号小的在右边
        // 右边的数据放到byte数组中序号小的位置
        byte[] bytes = new byte[Short.BYTES];
        bytes[0] = (byte) (intValue & 0xff);
        bytes[1] = (byte) ((intValue >> Byte.SIZE) & 0xff);
        bytes[2] = (byte) ((intValue >> Byte.SIZE * 2) & 0xff);
        bytes[3] = (byte) ((intValue >> Byte.SIZE * 3) & 0xff);
        return bytes;
    }

    
    public static byte[] intToBytesBigEndian(short intValue) {
        // byte数组中序号大的在右边
        // 右边的数据放到byte数组中序号大的位置
        byte[] bytes = new byte[Short.BYTES];
        bytes[3] = (byte) (intValue & 0xff);
        bytes[2] = (byte) ((intValue >> Byte.SIZE) & 0xff);
        bytes[1] = (byte) ((intValue >> Byte.SIZE * 2) & 0xff);
        bytes[0] = (byte) ((intValue >> Byte.SIZE * 3) & 0xff);
        return bytes;
    }
bytes》short
    
    public static short bytesToShortLittleEndian(byte[] bytes) {
        // byte数组中序号小的在右边
        return (short) (bytes[0] & 0xFF | (bytes[1] & 0xFF) << 8);
    }

    
    public static short bytesToShortBigEndian(byte[] bytes) {
        // byte数组中序号大的在右边
        return (short) (bytes[1] & 0xFF | (bytes[0] & 0xFF) << 8);
    }
short》bytes
    
    public static byte[] shortToBytesLittleEndian(short shortValue) {
        // byte数组中序号小的在右边
        // 右边的数据放到byte数组中序号小的位置
        byte[] bytes = new byte[Short.BYTES];
        bytes[0] = (byte) (shortValue & 0xff);
        bytes[1] = (byte) ((shortValue >> Byte.SIZE) & 0xff);
        return bytes;
    }

    
    public static byte[] shortToBytesBigEndian(short shortValue) {
        // byte数组中序号大的在右边
        // 右边的数据放到byte数组中序号大的位置
        byte[] bytes = new byte[Short.BYTES];
        bytes[1] = (byte) (shortValue & 0xff);
        bytes[0] = (byte) ((shortValue >> Byte.SIZE) & 0xff);
        return bytes;
    }
bytes》long
    
    public static long bytesToLongLittleEndian(byte[] bytes) {
        // byte数组中序号小的在右边
        long values = 0;
        for (int i = (Long.BYTES - 1); i >= 0; i--) {
            values <<= Byte.SIZE;
            values |= (bytes[i] & 0xff);
        }
        return values;
    }

    
    public static long bytesToLongBigEndian(byte[] bytes) {
        // byte数组中序号大的在右边
        long values = 0;
        for (int i = 0; i < Long.BYTES; i++) {
            values <<= Byte.SIZE;
            values |= (bytes[i] & 0xff);
        }
        return values;
    }
long》bytes
    
    public static byte[] longToBytesLittleEndian(long longValue) {
        // byte数组中序号小的在右边
        // 右边的数据放到byte数组中序号小的位置
        byte[] result = new byte[Long.BYTES];
        for (int i = 0; i < result.length; i++) {
            result[i] = (byte) (longValue & 0xFF);
            longValue >>= Byte.SIZE;
        }
        return result;
    }

    
    public static byte[] longToBytesBigEndian(long longValue) {
        // byte数组中序号大的在右边
        // 右边的数据放到byte数组中序号大的位置
        byte[] result = new byte[Long.BYTES];
        for (int i = (result.length - 1); i >= 0; i--) {
            result[i] = (byte) (longValue & 0xFF);
            longValue >>= Byte.SIZE;
        }
        return result;
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/940021.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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