byte[]数组和int之间的转换

本文介绍两种实现int类型与byte[]相互转换的方法,包括低位在前和高位在前的不同顺序,并提供了具体实现代码。

这里简单记录下两种转换方式:

第一种:

1、int与byte[]之间的转换(类似的byte short,long型)

 /** 
     * 将int数值转换为占四个字节的byte数组,本方法适用于(低位在前,高位在后)的顺序。 和bytesToInt()配套使用
     * @param value 
     *            要转换的int值
     * @return byte数组
     */  
	public static byte[] intToBytes( int value ) 
	{ 
		byte[] src = new byte[4];
		src[3] =  (byte) ((value>>24) & 0xFF);
		src[2] =  (byte) ((value>>16) & 0xFF);
		src[1] =  (byte) ((value>>8) & 0xFF);  
		src[0] =  (byte) (value & 0xFF);				
		return src; 
	}
	 /** 
     * 将int数值转换为占四个字节的byte数组,本方法适用于(高位在前,低位在后)的顺序。  和bytesToInt2()配套使用
     */  
	public static byte[] intToBytes2(int value) 
	{ 
		byte[] src = new byte[4];
		src[0] = (byte) ((value>>24) & 0xFF);
		src[1] = (byte) ((value>>16)& 0xFF);
		src[2] = (byte) ((value>>8)&0xFF);  
		src[3] = (byte) (value & 0xFF);		
		return src;
	}

byte[]转int

 /** 
     * byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序,和和intToBytes()配套使用
     *  
     * @param src 
     *            byte数组 
     * @param offset 
     *            从数组的第offset位开始 
     * @return int数值 
     */  
	public static int bytesToInt(byte[] src, int offset) {
		int value;	
		value = (int) ((src[offset] & 0xFF) 
				| ((src[offset+1] & 0xFF)<<8) 
				| ((src[offset+2] & 0xFF)<<16) 
				| ((src[offset+3] & 0xFF)<<24));
		return value;
	}
	
	 /** 
     * byte数组中取int数值,本方法适用于(低位在后,高位在前)的顺序。和intToBytes2()配套使用
     */
	public static int bytesToInt2(byte[] src, int offset) {
		int value;	
		value = (int) ( ((src[offset] & 0xFF)<<24)
				|((src[offset+1] & 0xFF)<<16)
				|((src[offset+2] & 0xFF)<<8)
				|(src[offset+3] & 0xFF));
		return value;
	}

第二种:

1、int与byte[]之间的转换(类似的byte short,long型)

	 /** 
     * 将int数值转换为占四个字节的byte数组,本方法适用于(低位在前,高位在后)的顺序。 
     * @param value 
     *            要转换的int值
     * @return byte数组
     */  
	public static byte[] intToBytes(int value) 
	{ 
		byte[] byte_src = new byte[4];
		byte_src[3] = (byte) ((value & 0xFF000000)>>24);
		byte_src[2] = (byte) ((value & 0x00FF0000)>>16);
		byte_src[1] = (byte) ((value & 0x0000FF00)>>8);  
		byte_src[0] = (byte) ((value & 0x000000FF));		
		return byte_src;
	}

byte[]转int

	 /** 
     * byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序。
     *  
     * @param ary 
     *            byte数组 
     * @param offset 
     *            从数组的第offset位开始 
     * @return int数值 
     */  
	public static int bytesToInt(byte[] ary, int offset) {
		int value;	
		value = (int) ((ary[offset]&0xFF) 
				| ((ary[offset+1]<<8) & 0xFF00)
				| ((ary[offset+2]<<16)& 0xFF0000) 
				| ((ary[offset+3]<<24) & 0xFF000000));
		return value;
	}


在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,否则可能导致数据丢失或异常。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值