在 Java 中将字节数组转换为整数,反之亦然
我想用Java将一些数据存储到字节数组中。基本上只是数字,每个数字最多占用 2 个字节。
我想知道如何将整数转换为 2 字节长的字节数组,反之亦然。我在谷歌上找到了很多解决方案,但大多数都没有解释代码中发生的情况。有很多我不太理解的变化的东西,所以我希望有一个基本的解释。
I want to store some data into byte arrays in Java. Basically just numbers which can take up to 2 Bytes per number.
I'd like to know how I can convert an integer into a 2 byte long byte array and vice versa. I found a lot of solutions googling but most of them don't explain what happens in the code. There's a lot of shifting stuff I don't really understand so I would appreciate a basic explanation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
使用
java.lang.Class 中找到的类。 nio
命名空间,特别是ByteBuffer
。它可以为您完成所有工作。Use the classes found in the
java.nio
namespace, in particular, theByteBuffer
. It can do all the work for you.当将有符号字节打包到 int 中时,每个字节都需要被屏蔽,因为由于算术提升规则(在 JLS,转换和提升中描述),它被符号扩展为 32 位(而不是零扩展)。
Joshua Bloch 和 Neal Gafter 在 Java Puzzlers(“A Big Delight in Every Byte”)中描述了一个与此相关的有趣谜题。将字节值与 int 值进行比较时,该字节会被符号扩展为 int,然后将该值与另一个 int 进行比较。
请注意,所有数字类型在 Java 中都是有符号的,除了 char 是 16 位无符号整数类型之外。
When packing signed bytes into an int, each byte needs to be masked off because it is sign-extended to 32 bits (rather than zero-extended) due to the arithmetic promotion rule (described in JLS, Conversions and Promotions).
There's an interesting puzzle related to this described in Java Puzzlers ("A Big Delight in Every Byte") by Joshua Bloch and Neal Gafter . When comparing a byte value to an int value, the byte is sign-extended to an int and then this value is compared to the other int
Note that all numeric types are signed in Java with exception to char being a 16-bit unsigned integer type.
您还可以使用 BigInteger 来表示可变长度字节。您可以将其转换为 long、int 或short,无论哪种都适合您的需要。
或表示极性:
要返回字节:
虽然很简单,但我只是想指出,如果在循环中运行多次,这可能会导致大量垃圾收集。根据您的用例,这可能是一个问题。
You can also use BigInteger for variable length bytes. You can convert it to long, int or short, whichever suits your needs.
or to denote polarity:
To get bytes back just:
Although simple, I just wanted to point out that if you run this many times in a loop, this could lead to a lot of garbage collection. This may be a concern depending on your use case.
一个基本的实现是这样的:
为了理解一些东西,你可以阅读这篇 WP 文章: http:// en.wikipedia.org/wiki/Endianness
上面的源代码将输出
34 12 78 56 bc 9a
。前 2 个字节(34 12
)表示第一个整数,依此类推。上述源代码以小端格式对整数进行编码。A basic implementation would be something like this:
In order to understand things you can read this WP article: http://en.wikipedia.org/wiki/Endianness
The above source code will output
34 12 78 56 bc 9a
. The first 2 bytes (34 12
) represent the first integer, etc. The above source code encodes integers in little endian format.通常,番石榴可以满足您的需求。
要从字节数组转换为 int:
Ints.fromBytesArray
,doc 此处要从 int 转换为字节数组:
Ints.toByteArray
,doc 此处As often, guava has what you need.
To go from byte array to int:
Ints.fromBytesArray
, doc hereTo go from int to byte array:
Ints.toByteArray
, doc here无需使用任何外部或晦涩的库或应用按位魔法(有点难以阅读),您可以使用
ByteArrayOutputStream
与DataOutputStream
使用它的
writeInt(int)
方法将int
写入流中:类似地使用
ByteArrayInputStream
与
DataInputStream
使用它的int readInt()
方法从流中读取 int:更新: 如果是整数数字小到足以容纳 2 个字节,然后才使用 writeShort 和 readShort 方法而不是
writeInt
和readInt
方法。Without using any external or obscure library or applying bitwise magic (bit difficult to read), you can use
ByteArrayOutputStream
combined withDataOutputStream
to write anint
into the stream like this using it'swriteInt(int)
method:Similarly use
ByteArrayInputStream
combined withDataInputStream
to read an int from the stream like this using it'sint readInt()
method:Update: If integer number is small enough to be fit into 2 bytes only then use writeShort and readShort methods instead of
writeInt
andreadInt
methods.有人要求必须从位读取,假设您必须仅读取 3 位,但您需要有符号整数,然后使用以下内容:
幻数
3
可以替换为 < 的数量您正在使用的strong>位(不是字节)。Someone with a requirement where they have to read from bits, lets say you have to read from only 3 bits but you need signed integer then use following:
The magic number
3
can be replaced with the number of bits (not bytes) you are using.我认为这是转换为 int 的最佳模式,
首先将字节转换为字符串,
下一步是转换为 int
但字节的范围为 -128 到 127,因此,我认为最好使用范围 0 到 255,你只需要为此:
i think this is a best mode to cast to int
first comvert byte to String
next step is comvert to a int
but byte is in rage of -128 to 127 for this reasone, i think is better use rage 0 to 255 and you only need to do this: