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

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

一、位运算

运算符含义说明
&对应位都是1,结果为1,否则为0
|对应位都是0,结果为0,否则为1
~取反每一位变相反位,即0变成1,1变成0
^异或对应位值相同,结果为0,否则为1
<<左移位低位补0
>>右移位保留符号位,0为正,1为负
>>>无符号右移位高位补0

位逻辑运算示例

ABA&BA|B~AA^B
000010
010111
100101
111100

二、 端序

端序:字节序

byte数据顺序与内存地址高低的关系

小端(Litter Endian):

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

大端(Big Endian):

  • 高字节在后,byte数组中序号大的在右边

网络字节序(Network Order):

  • TCP/IP各层协议将字节序定义为Big Endian,因此TCP/IP协议中使用的字节序通常称之为网络字节序。

主机字节序(Host Order)

  • 整数在内存中保存的顺序,它通常遵循Little Endian规则(不一定,要看主机的CPU架构)。所以当两台主机之间要通过TCP/IP协议进行通信的时候就需要调用相应的函数进行主机序列(Little Endian)和网络序(Big Endian)的转换。

三、byte数组与整数之间的转换

3.1、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; 
	}

3.2、int=>bytes

	/*小端,低字节在后*/
    public static byte[] intToBytesLittleEndian(int intValue) {
        // byte数组中序号小的在右边
        // 右边的数据放到byte数组中序号小的位置
        byte[] bytes = new byte[4];
        bytes[0] = (byte) (intValue & 0xff);
        bytes[1] = (byte) ((intValue >> 8) & 0xff);
        bytes[2] = (byte) ((intValue >> 16) & 0xff);
        bytes[3] = (byte) ((intValue >> 24) & 0xff);
        return bytes;
    }
    /*大端,高字节在后*/
    public static byte[] intToBytesBigEndian(int intValue) {
        // byte数组中序号大的在右边
        // 右边的数据放到byte数组中序号大的位置
        byte[] bytes = new byte[4];
        bytes[3] = (byte) (intValue & 0xff);
        bytes[2] = (byte) ((intValue >> 8) & 0xff);
        bytes[1] = (byte) ((intValue >> 16) & 0xff);
        bytes[0] = (byte) ((intValue >> 24) & 0xff);
        return bytes;
    }

3.3、bytes=>short(int16)

	/*小端,低字节在后*/
    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);
    }

3.4、short(int16)=>bytes

	/*小端,低字节在后*/
    public static byte[] shortToBytesLittleEndian(short shortValue) {
        // byte数组中序号小的在右边
        // 右边的数据放到byte数组中序号小的位置
        byte[] bytes = new byte[2];
        bytes[0] = (byte) (shortValue & 0xff);
        bytes[1] = (byte) ((shortValue >> 8) & 0xff);
        return bytes;
    }
    /*大端,高字节在后*/
    public static byte[] shortToBytesBigEndian(short shortValue) {
        // byte数组中序号大的在右边
        // 右边的数据放到byte数组中序号大的位置
        byte[] bytes = new byte[2];
        bytes[1] = (byte) (shortValue & 0xff);
        bytes[0] = (byte) ((shortValue >> 8) & 0xff);
        return bytes;
    }

3.5、bytes=>long(int64)

	/*小端,低字节在后*/
    public static long bytesToLongLittleEndian(byte[] bytes) {
        // byte数组中序号小的在右边
        long values = 0;
        for (int i = (16 - 1); i >= 0; i--) {
            values <<= 8;
            values |= (bytes[i] & 0xff);
        }
        return values;
    }

    /*大端,高字节在后*/
    public static long bytesToLongBigEndian(byte[] bytes) {
        // byte数组中序号大的在右边
        long values = 0;
        for (int i = 0; i < 16; i++) {
            values <<= 8;
            values |= (bytes[i] & 0xff);
        }
        return values;
    }

3.6、long(int64)=>bytes

	/*小端,低字节在后*/
    public static byte[] longToBytesLittleEndian(long longValue) {
        // byte数组中序号小的在右边
        // 右边的数据放到byte数组中序号小的位置
        byte[] result = new byte[16];
        for (int i = 0; i < result.length; i++) {
            result[i] = (byte) (longValue & 0xFF);
            longValue >>= 8;
        }
        return result;
    }

    /*大端,高字节在后*/
    public static byte[] longToBytesBigEndian(long longValue) {
        // byte数组中序号大的在右边
        // 右边的数据放到byte数组中序号大的位置
        byte[] result = new byte[16];
        for (int i = (result.length - 1); i >= 0; i--) {
            result[i] = (byte) (longValue & 0xFF);
            longValue >>= 8;
        }
        return result;
    }
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值