问题

我想用Java将一些数据存储到字节数组中。基本上只是每个数字最多可以占用2个字节的数字。

我想知道如何将整数转换为2字节长字节数组,反之亦然。我发现了很多解决方案谷歌搜索,但大多数解释不到代码中发生的事情。有很多变化的东西,我真的不明白,所以我希望得到一个基本的解释。


#1 热门回答(175 赞)

使用在java.nionamespace中找到的类,特别是ByteBuffer。它可以为你完成所有工作。

byte[] arr = { 0x00, 0x01 };
ByteBuffer wrapped = ByteBuffer.wrap(arr); // big-endian by default
short num = wrapped.getShort(); // 1

ByteBuffer dbuf = ByteBuffer.allocate(2);
dbuf.putShort(num);
byte[] bytes = dbuf.array(); // { 0, 1 }

#2 热门回答(100 赞)

byte[] toByteArray(int value) {
     return  ByteBuffer.allocate(4).putInt(value).array();
}

byte[] toByteArray(int value) {
    return new byte[] { 
        (byte)(value >> 24),
        (byte)(value >> 16),
        (byte)(value >> 8),
        (byte)value };
}

int fromByteArray(byte[] bytes) {
     return ByteBuffer.wrap(bytes).getInt();
}
// packing an array of 4 bytes to an int, big endian
int fromByteArray(byte[] bytes) {
     return bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF);
}

将有符号字节打包到int中时,需要屏蔽掉每个字节,因为由于算术提升规则(在JLS,转换和促销中描述),它被符号扩展为32位(而不是零扩展)。

Joshua Bloch和Neal Gafter在Java Puzzlers("每个字节中的一个大喜悦")中描述了一个有趣的谜题。将字节值与int值进行比较时,将字节符号扩展为int,然后将此值与另一个int进行比较

byte[] bytes = (…)
if (bytes[0] == 0xFF) {
   // dead code, bytes[0] is in the range [-128,127] and thus never equal to 255
}

请注意,所有数字类型都是用Java签名的,但char是16位无符号整数类型。


#3 热门回答(37 赞)

你还可以将BigInteger用于可变长度字节。你可以将其转换为long,int或short,以满足你的需求。

new BigInteger(bytes).intValue();

或表示极性:

new BigInteger(1, bytes).intValue();

要恢复字节:

new BigInteger(bytes).toByteArray()

原文链接