将原始字节值转换为 Java 类型
我在将原始字节值转换为 java 类型时遇到问题。我通过数据报套接字接收字节作为字节数组。我确切地知道哪些字节意味着什么,但我不知道如何适当地转换它们(我的意思是我知道偏移量,但不知道我认为我收到的内容是否正确;))。
例如,我想将16位unsigned Short转换为java int类型。我在网上找到了一些例子,一个是:
public int getUShort(byte[] bytes, int offset) {
int b0 = bytes[offset] & oxFF;
int b1 = bytes[offset + 1] & oxFF;
return (b1 << 8) + (b0 << 0);
另一个是相同的,但最后一行是:
return (b0 << 8) + b1;
当然它给出了不同的结果。哪一个是正确的?您能否给我一个有效的示例,如何执行相同的操作,但对于无符号长整型?
先感谢您!
I have a problem with converting raw-bytes values into java types. I am receiving bytes by a datagram socket as a bytes array. I know exactly which bytes means what, but I don't know how to convert them appropriately (I mean I know offsets, but don't know if what I think I received is correct ;)).
For example, I want to convert 16 bit unsigned short into java int type. I found some examples in the web, the one is:
public int getUShort(byte[] bytes, int offset) {
int b0 = bytes[offset] & oxFF;
int b1 = bytes[offset + 1] & oxFF;
return (b1 << 8) + (b0 << 0);
Another one is the same but the last line is:
return (b0 << 8) + b1;
Of course it gives different results. Which one is correct? Can you please give me also a valid example how to do the same but for an unsigned long?
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不久前我必须做一些与此类似的工作,我发现完成此类工作的最佳方法是使用
ByteBuffer
及其到DoubleBuffer
的转换,< code>LongBuffer 等。您可以通过调用将字节数组转换为ByteBuffer
从那里,您可以获得字节视图作为
int
列表code>s 通过调用,然后您可以通过调用转换字节
这些类还对批量获取操作有很多支持,因此如果您知道您正在通过网络接收一大堆相同类型的数据您可以非常快速地进行转换。
如果可能的话,另一种方法是使用某种基于 Java 的序列化通过网络发送数据。这使您可以使用流编写器类更轻松地进行转换。当然,这可能不可用,特别是当您与非 Java 服务器通信时,但它可能值得探索。
I had to do some work similar to this a while back and I found that the best way to do this sort of work is to use
ByteBuffer
and its conversions toDoubleBuffer
,LongBuffer
, etc. You can convert an array of bytes into aByteBuffer
by callingFrom there, you can get a view of the bytes as a list of
int
s by callingand you can then convert the bytes by calling
These classes also have lots of support for bulk get operations, so if you know for a fact that you're receiving a whole bunch of data of the same type over the network you can do the conversions very quickly.
An alternative approach would be, if at all possible, to use some sort of Java-based serialization to send the data over the network. This allows you to do the conversions much more easily using the stream writer classes. Of course, this might not be available, especially if you're communicating with a non-Java server, but it might be worth exploring.
您可以使用 DataInputStream 或 ByteBuffer 来读取各种类型。对于大多数操作,您可以将有符号类型用作无符号值。我编写了一个简单的类来说明如何使用带符号的类型,就好像它们是无符号的无符号
You can use DataInputStream or ByteBuffer to read the various types. You can use signed types as unsigned values for most operations just the same. I have written a simple class to illustrate how you can use the signed types as if they were unsigned Unsigned
这件事确实迟到了。
根据数据的字节序,两者都是正确的。请参阅此处:http://en.wikipedia.org/wiki/Endianness
Really late on this one.
Both are correct based on the endianess of the data. See here: http://en.wikipedia.org/wiki/Endianness