将原始字节值转换为 Java 类型

发布于 2024-10-12 04:34:27 字数 538 浏览 1 评论 0原文

我在将原始字节值转换为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戈亓 2024-10-19 04:34:28

不久前我必须做一些与此类似的工作,我发现完成此类工作的最佳方法是使用 ByteBuffer 及其到 DoubleBuffer 的转换,< code>LongBuffer 等。您可以通过调用将字节数组转换为 ByteBuffer

ByteBuffer myBuffer = ByteBuffer.wrap(myRawArray);

从那里,您可以获得字节视图作为 int 列表code>s 通过调用

IntBuffer myIntBuffer = myBuffer.asIntBuffer();

,然后您可以通过调用转换字节

int nextInt = myIntBuffer.get();

这些类还对批量获取操作有很多支持,因此如果您知道您正在通过网络接收一大堆相同类型的数据您可以非常快速地进行转换。

如果可能的话,另一种方法是使用某种基于 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 to DoubleBuffer, LongBuffer, etc. You can convert an array of bytes into a ByteBuffer by calling

ByteBuffer myBuffer = ByteBuffer.wrap(myRawArray);

From there, you can get a view of the bytes as a list of ints by calling

IntBuffer myIntBuffer = myBuffer.asIntBuffer();

and you can then convert the bytes by calling

int nextInt = myIntBuffer.get();

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.

对不⑦ 2024-10-19 04:34:28

您可以使用 DataInputStream 或 ByteBuffer 来读取各种类型。对于大多数操作,您可以将有符号类型用作无符号值。我编写了一个简单的类来说明如何使用带符号的类型,就好像它们是无符号的无符号

ByteBuffer b = ByteBuffer.wrap(bytes);
short s = b.getShort();
long l = b.getLong();

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

ByteBuffer b = ByteBuffer.wrap(bytes);
short s = b.getShort();
long l = b.getLong();
如若梦似彩虹 2024-10-19 04:34:28

这件事确实迟到了。

根据数据的字节序,两者都是正确的。请参阅此处:http://en.wikipedia.org/wiki/Endianness

return (b1 << 8) + b0;//little endian
return (b0 << 8) + b1;//big endian

Really late on this one.

Both are correct based on the endianess of the data. See here: http://en.wikipedia.org/wiki/Endianness

return (b1 << 8) + b0;//little endian
return (b0 << 8) + b1;//big endian
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文