杰瑞科技汇

Java中int如何转byte数组?

在 Java 中,将 int 类型转换为 byte 数组有几种常见的方法,主要区别在于字节的顺序(大端序/小端序),以下是几种实现方式:

Java中int如何转byte数组?-图1
(图片来源网络,侵删)

方法 1:使用 ByteBuffer(推荐)

ByteBuffer 是 Java NIO 提供的类,可以方便地进行字节序转换。

大端序(Big-Endian,高位在前)

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class IntToBytes {
    public static byte[] intToBytesBigEndian(int value) {
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.order(ByteOrder.BIG_ENDIAN); // 默认就是大端序,可省略
        buffer.putInt(value);
        return buffer.array();
    }
    public static void main(String[] args) {
        int num = 0x12345678;
        byte[] bytes = intToBytesBigEndian(num);
        // 输出: [18, 52, 86, 120] (对应 0x12, 0x34, 0x56, 0x78)
        for (byte b : bytes) {
            System.out.printf("%02X ", b);
        }
    }
}

小端序(Little-Endian,低位在前)

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class IntToBytes {
    public static byte[] intToBytesLittleEndian(int value) {
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        buffer.putInt(value);
        return buffer.array();
    }
    public static void main(String[] args) {
        int num = 0x12345678;
        byte[] bytes = intToBytesLittleEndian(num);
        // 输出: [120, 86, 52, 18] (对应 0x78, 0x56, 0x34, 0x12)
        for (byte b : bytes) {
            System.out.printf("%02X ", b);
        }
    }
}

方法 2:手动位移操作(不依赖 NIO)

通过位移和位运算手动拆分 int 的 4 个字节。

大端序(高位在前)

public class IntToBytes {
    public static byte[] intToBytesBigEndian(int value) {
        return new byte[] {
            (byte) (value >> 24),       // 最高字节(第 3 字节)
            (byte) (value >> 16),       // 第 2 字节
            (byte) (value >> 8),        // 第 1 字节
            (byte) value                // 最低字节(第 0 字节)
        };
    }
    public static void main(String[] args) {
        int num = 0x12345678;
        byte[] bytes = intToBytesBigEndian(num);
        // 输出: [18, 52, 86, 120]
        for (byte b : bytes) {
            System.out.printf("%02X ", b);
        }
    }
}

小端序(低位在前)

public class IntToBytes {
    public static byte[] intToBytesLittleEndian(int value) {
        return new byte[] {
            (byte) value,               // 最低字节(第 0 字节)
            (byte) (value >> 8),        // 第 1 字节
            (byte) (value >> 16),       // 第 2 字节
            (byte) (value >> 24)        // 最高字节(第 3 字节)
        };
    }
    public static void main(String[] args) {
        int num = 0x12345678;
        byte[] bytes = intToBytesLittleEndian(num);
        // 输出: [120, 86, 52, 18]
        for (byte b : bytes) {
            System.out.printf("%02X ", b);
        }
    }
}

方法 3:使用 DataOutputStream

通过 DataOutputStream 写入 int 到字节数组输出流中。

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class IntToBytes {
    public static byte[] intToBytes(int value) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        dos.writeInt(value);
        return baos.toByteArray();
    }
    public static void main(String[] args) throws IOException {
        int num = 0x12345678;
        byte[] bytes = intToBytes(num);
        // 默认是大端序,输出: [18, 52, 86, 120]
        for (byte b : bytes) {
            System.out.printf("%02X ", b);
        }
    }
}

反向操作:byte数组转int

如果需要将 byte[] 转回 int,可以使用类似的方法:

Java中int如何转byte数组?-图2
(图片来源网络,侵删)

使用 ByteBuffer

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class BytesToInt {
    public static int bytesToIntBigEndian(byte[] bytes) {
        ByteBuffer buffer = ByteBuffer.wrap(bytes);
        buffer.order(ByteOrder.BIG_ENDIAN);
        return buffer.getInt();
    }
    public static int bytesToIntLittleEndian(byte[] bytes) {
        ByteBuffer buffer = ByteBuffer.wrap(bytes);
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        return buffer.getInt();
    }
    public static void main(String[] args) {
        byte[] bigEndianBytes = {0x12, 0x34, 0x56, 0x78};
        byte[] littleEndianBytes = {0x78, 0x56, 0x34, 0x12};
        int bigEndianInt = bytesToIntBigEndian(bigEndianBytes);   // 0x12345678
        int littleEndianInt = bytesToIntLittleEndian(littleEndianBytes); // 0x12345678
        System.out.println("BigEndian: " + Integer.toHexString(bigEndianInt));
        System.out.println("LittleEndian: " + Integer.toHexString(littleEndianInt));
    }
}

方法 优点 缺点
ByteBuffer 代码简洁,支持字节序转换 依赖 NIO 包
手动位移 无依赖,性能较高 需要手动处理字节序和符号位问题
DataOutputStream 可扩展性强(可写入其他类型) 需要处理异常,性能稍低

推荐优先使用 ByteBuffer,代码清晰且不易出错。

分享:
扫描分享到社交APP
上一篇
下一篇