在 Java 中,将 int 类型转换为 byte 数组是一个常见的操作,特别是在处理网络协议、文件存储或低级数据格式时。int 占用 4 个字节(32 位),而 byte 占用 1 个字节(8 位),因此我们需要将 int 的 4 个字节分别提取出来,存入一个长度为 4 的 byte 数组中。

方法 1:使用位运算(推荐)
这是最直接且高效的方法,通过位移和位与操作来提取每个字节。
public static byte[] intToByteArray(int value) {
byte[] bytes = new byte[4];
bytes[0] = (byte) (value >> 24); // 最高位字节
bytes[1] = (byte) (value >> 16);
bytes[2] = (byte) (value >> 8);
bytes[3] = (byte) value; // 最低位字节
return bytes;
}
解释:
value >> 24:将int的最高 8 位(第 31-24 位)移动到最低 8 位,然后通过(byte)强制类型转换截取这 8 位。value >> 16:将第 23-16 位移到最低 8 位。value >> 8:将第 15-8 位移到最低 8 位。value:直接截取最低 8 位(第 7-0 位)。
方法 2:使用 ByteBuffer(NIO)
Java NIO(New I/O)提供了 ByteBuffer 类,可以更简洁地完成转换。
import java.nio.ByteBuffer;
public static byte[] intToByteArray(int value) {
ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.putInt(value);
return buffer.array();
}
解释:

ByteBuffer.allocate(4):分配一个 4 字节的缓冲区。buffer.putInt(value):将int写入缓冲区。buffer.array():将缓冲区的内容转换为byte数组。
方法 3:使用 ByteArrayOutputStream 和 DataOutputStream
这种方法通过流的方式写入数据,适用于更复杂的场景。
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public static byte[] intToByteArray(int value) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(4);
DataOutputStream dos = new DataOutputStream(baos);
dos.writeInt(value);
return baos.toByteArray();
}
解释:
ByteArrayOutputStream:在内存中创建一个字节数组输出流。DataOutputStream:提供写入基本数据类型的方法(如writeInt)。baos.toByteArray():将流中的字节数组取出。
字节序(Byte Order)
需要注意的是,Java 的 int 到 byte 数组的转换默认是大端序(Big-Endian),即最高位字节(MSB)存放在数组的最前面(bytes[0]),如果需要小端序(Little-Endian),可以调整顺序或使用 ByteBuffer 的 order() 方法。
小端序示例(使用 ByteBuffer):
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public static byte[] intToByteArrayLittleEndian(int value) {
ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.order(ByteOrder.LITTLE_ENDIAN); // 设置为小端序
buffer.putInt(value);
return buffer.array();
}
完整示例代码
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class IntToByteArrayExample {
public static void main(String[] args) {
int value = 0x12345678;
// 方法 1:位运算
byte[] bytes1 = intToByteArray(value);
printByteArray(bytes1); // 输出: [12, 34, 56, 78] (大端序)
// 方法 2:ByteBuffer(大端序)
byte[] bytes2 = intToByteArray(value);
printByteArray(bytes2); // 输出: [12, 34, 56, 78]
// 方法 3:ByteBuffer(小端序)
byte[] bytes3 = intToByteArrayLittleEndian(value);
printByteArray(bytes3); // 输出: [78, 56, 34, 12]
}
// 方法 1:位运算
public static byte[] intToByteArray(int value) {
return new byte[]{
(byte) (value >> 24),
(byte) (value >> 16),
(byte) (value >> 8),
(byte) value
};
}
// 方法 2:ByteBuffer(大端序)
public static byte[] intToByteBuffer(int value) {
ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.putInt(value);
return buffer.array();
}
// 方法 3:ByteBuffer(小端序)
public static byte[] intToByteArrayLittleEndian(int value) {
ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putInt(value);
return buffer.array();
}
// 辅助方法:打印字节数组
public static void printByteArray(byte[] bytes) {
System.out.print("[");
for (int i = 0; i < bytes.length; i++) {
System.out.print(String.format("%02X", bytes[i]));
if (i < bytes.length - 1) {
System.out.print(", ");
}
}
System.out.println("]");
}
}
输出结果:
[12, 34, 56, 78]
[12, 34, 56, 78]
[78, 56, 34, 12]
| 方法 | 优点 | 缺点 |
|---|---|---|
| 位运算 | 高效、不依赖额外类 | 需要手动处理字节序 |
| ByteBuffer | 代码简洁、支持字节序 | 需要导入 NIO 包 |
| DataOutputStream | 适合流式操作 | 性能稍低、需要处理异常 |
推荐使用 方法 1(位运算) 或 方法 2(ByteBuffer),根据具体需求选择是否需要处理字节序。

