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

本文介绍了Java中如何通过位运算进行字节与整数之间的转换,包括小端和大端字节序的处理。提供了byte数组转int、int转byte数组,以及short和long类型的转换方法。同时,阐述了字节序的概念,如网络字节序(BigEndian)和主机字节序,强调了在跨平台通信中的重要性。

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;
    }
Java中,字节数组(`byte[]`)整数(`int`)之间相互转换是常见的操作,尤其是在处理网络传输、文件存储或底层数据结构时。以下详细说明如何实现这两种转换。 ### 一、将`int`转换为`byte[]` #### 方法1:使用位运算 可以通过位运算符(`&`、`>>>`)将一个`int`值分解为4个字节,并存入`byte[]`数组中。以下是一个标准的实现方式: ```java public static byte[] intToBytes(int value) { byte[] result = new byte[4]; result[0] = (byte) (value & 0xFF); // 取最低位字节 result[1] = (byte) ((value >> 8) & 0xFF); // 取次低位字节 result[2] = (byte) ((value >> 16) & 0xFF); // 取次高位字节 result[3] = (byte) ((value >> 24) & 0xFF); // 取最高位字节 return result; } ``` 该方法采用低位在前(小端序)的方式进行存储[^3]。 #### 方法2:使用`DataOutputStream` 也可以借助`DataOutputStream`将`int`写入`ByteArrayOutputStream`,然后获取字节数组: ```java public static byte[] intToByteArray(int a) throws IOException { ByteArrayOutputStream bao = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bao); dos.writeInt(a); return bao.toByteArray(); } ``` 这种方式更为简洁,适用于处理多个数据类型混合写入的情况[^2]。 ### 二、将`byte[]`转换为`int` #### 方法1:使用位运算 从一个长度为4的`byte[]`中提取`int`值,可以使用位移和按位操作: ```java public static int bytesToInt(byte[] bytes) { return ((bytes[0] & 0xFF)) | ((bytes[1] & 0xFF) << 8) | ((bytes[2] & 0xFF) << 16) | ((bytes[3] & 0xFF) << 24); } ``` 该方法也遵循小端序(低位在前)的规则[^3]。 #### 方法2:使用`DataInputStream` 同样可以使用`DataInputStream`来读取字节数组中的`int`值: ```java public static int byteArrayToInt(byte[] data) throws IOException { ByteArrayInputStream bai = new ByteArrayInputStream(data); DataInputStream dis = new DataInputStream(bai); return dis.readInt(); } ``` 此方法适用于从流中读取数据或处理已知格式的字节数组[^2]。 ### 三、注意事项 - `byte`是带符号的(-128~127),在转换为`int`时需要进行`& 0xFF`操作以确保高位补零。 - 字节序(大端或小端)会影响转换结果,上述方法均采用小端序(LSB first)。 - 输入的`byte[]`长度必须为4,否则可能导致数据丢失或异常。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值