Java将4个字节转换为int
我想知道这里记录的解决方案是否仍然是解决方案或者还有其他方法从 4 个字节获取 int 吗?
谢谢。
编辑:我从套接字获取字节[] .read
编辑:int recvMsgSize = in.read(Data, 0, BufferSize);
如果recvMsgSize为-1,我知道连接已被删除。
当我使用 DataInputStream 而不是 InputStream 时,如何检测到这一点?
谢谢。
编辑:对于接受正确答案作为溜溜球者表示歉意。但在 mihi 更新最终回复后,该方法似乎很可靠,并且减少了扩展编码,在我看来是最佳实践。
i was wondering if the solution for this documented here is still the solution or is there any other way getting an int from 4 bytes?
thank you.
EDIT: im getting the byte[] from sockets .read
EDIT: int recvMsgSize = in.read(Data, 0, BufferSize);
if recvMsgSize is -1 i know the connection has been dropped.
how do i detect this when im using DataInputStream instead of InputStream?
thanks.
EDIT: apologies for being a yoyo regarding accepting the right answer. but after mihi's updated final response, it would appear that the method is solid and cuts down extended coding and in my opinion best practice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您必须非常小心任何扩大转换和数字提升,但下面的代码将 4
byte
转换为int
:另请参阅
&掩码0xFF
- 如果您使用byte
,您可能最终会做很多这样的事情,因为所有算术运算都会提升为int
(或int
) >长)You have to be very careful with any widening conversion and numeric promotion, but the code below converts 4
byte
intoint
:See also
& 0xFF
-- you'll probably end up doing a lot of this if you're working withbyte
since all arithmetic operations promote toint
(orlong
)如果您已将它们存储在
byte[]
数组中,则可以使用:或者,如果您有 Google 的 guava-libraries 在你的类路径上,你有快捷方式:
它的优点是你可以为其他类型提供同样好的 API(
Longs.fromByteArray
、Shorts.fromByteArray
等)。If you have them already in a
byte[]
array, you can use:or, if you have Google's guava-libraries on your classpath, you have the shortcut:
which has the advantage that you have similarly nice APIs for other types (
Longs.fromByteArray
,Shorts.fromByteArray
, etc).取决于您从何处获取这 4 个字节:
http://docs.oracle.com/javase/7/docs/api/java/io/DataInput.html#readInt()
http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer。 html#getInt(int)
您当然仍然可以手动执行此操作,但在大多数情况下使用其中之一(如果您必须转换具有大量字节的字节数组,您可能需要使用
例如,围绕
)更容易。ByteArrayInputStream
的 DataInputStream编辑:如果您需要更改字节顺序,则必须使用 ByteBuffer,或者自己反转字节,或者自己进行转换,因为 DataInput 不支持更改字节顺序。
Edit2:当您从套接字输入流获取它们时,我会将其包装到
DataInputStream
中,并使用它来读取各种数据。特别是因为 InputStream.read(byte[]) 不能保证填充整个字节数组... DataInputStream.readFully 可以。Edit3:当从流中多次读取时,它们将继续读取您停止的地方,即aByte将是字节0,anInt将是字节1到4,anInt将是字节5到8,等等。 readFully 将在所有这些之后继续读取,并会阻塞,直到读取完
lotOfbytes
。当流停止(连接断开)时,您将得到 EOFException 而不是 -1,因此如果您得到 -1,则 int 实际上是 -1。
如果您根本不想解析任何字节,可以跳过()它们。使用 DataInputStream 不可能以两种不同的方式解析一个字节(即首先从字节 0 到 3 读取一个 int,然后从字节 2 到 5 读取一个 int),但通常也不需要。
示例:
希望这能回答您的其他问题。
Depending on where you get those 4 bytes from:
http://docs.oracle.com/javase/7/docs/api/java/io/DataInput.html#readInt()
http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html#getInt(int)
You can of course still do it manually, but in most cases using one of those (if you have to convert a byte array with lots of bytes, you might want to use a
DataInputStream
around aByteArrayInputStream
for example) is easier.Edit: If you need to change the endianness, you will have to use a ByteBuffer, or reverse the bytes yourself, or do the conversion yourself, as DataInput does not support changing the endianness.
Edit2: When you get them from the socket input stream, I'd wrap that one into a
DataInputStream
and use it for reading all kinds of data. Especially since InputStream.read(byte[]) will not guarantee to fill the whole byte array... DataInputStream.readFully does.Edit3: When reading multiple times from a stream, they will continue reading where you stopped, i. e. aByte will be byte 0, anInt will be bytes 1 to 4, anotherInt will be bytes 5 to 8, etc. readFully will read on after all that and will block until it has read
lotOfbytes
.When the stream stops (the connection drops) you will get
EOFException
instead of -1, so if you get -1, the int really was -1.If you do not want to parse any bytes at all, you can skip() them. Parsing one byte in 2 different ways is not possible with DataInputStream (i. e. read first an int from byte 0 to 3, then one from byte 2 to 5), but usually not needed either.
Example:
Hope this answers your additional questions.
功能风格的解决方案(只是为了多样性,恕我直言,使用起来不太方便):
A solution in functional style (just for variety, imho not very convinient in use):
正如 mihi 所说,这取决于您从哪里获取这些字节,但以下代码可能有用:
As mihi said, it depends on where you are getting those bytes from, but this code might be of use: