网络字节顺序到java中的主机字节顺序 [英] network byte order to host byte order in java

查看:326
本文介绍了网络字节顺序到java中的主机字节顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难解释从C服务器到我的java应用程序的响应。希望C服务器以BIG_ENDIAN格式响应我的java,我的问题是如何将响应转换为 Integer 。我正在粘贴我从C服务器得到的响应,并想知道如何在java中执行be64toh功能。

I'm stuck with interpreting a response from a C server to my java application. Hoping that the C server is responding to my java in BIG_ENDIAN format, my question is how would I convert the response to an Integer. I'm pasting the response I got from the the C server and wondering how would I do "be64toh" function in java.

来自C服务器的响应 -

Response from the C server -


^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ H ^ @ ^ @ ^ @ ^ @ ^ @ ^ A ^ A ^ B ^ @ ^ @ ^ @ ^ N ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ H ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ A ^ @ ^ @ ^ @ ^ K ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @< 80> ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ B ^ ^ @ ^ @ ^ @ ^ AYour
请求成功在我们这里注册并处理
。^ @ ^ @ ^ @ ^ @ ^ D ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @¨¨½f^ @ ^ @ ^ @ ^ @ ^ @mX @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ N ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ H

^@^@^@^@^@^@^@^@^@^@^@^@^@^@^H^@^@^@^@^@^A^A^B^@^@^@^N^@^@^@^@^@^@^@^@^@^@^@^H^@^@^@^@^@^@^@^A^@^@^@^K^@^@^@^@^@^@^@^@^@^@^@<80>^@^@^@^@^@^@^BÃ^@^@^@^AYour request is successfully registered with us and is under processing.^@^@^@^@^D^@^@^@^@^@^@^@¨ýf^@^@^@^@^@mX@^@^@^@^@^@^@^@^@^N^@^@^@^@^@^@^@^@^@^@^@^H

我不知道如何解释不可读的字节,当转换为十六进制时,大多数都是 null 但其中很少有数据填充,就像 ^ H ^ A 等。你能帮忙吗?我找到了这个解决方案。

I'm not sure how to interpret the non readable bytes and when converted to hexadecimal most of them are null but few of them are filled with data just like the ^H, ^A etc.. Can you please help me find a solution for this one.

推荐答案

假设你有一个java.nio.SocketChannel来读取,你可以使用一个ByteBuffer进行转换:

Suppose you have a java.nio.SocketChannel to read from, you can use a ByteBuffer to do the conversion:

private static final int BUFFER_SIZE = 1024;

ByteBuffer readBuffer = ByteBuffer.allocate(BUFFER_SIZE).order(ByteOrder.BIG_ENDIAN);
int bytesRead = socketChannel.read(readBuffer);
readBuffer.flip();

现在您可以从缓冲区中读取数据,例如:

Now you can read your data out of the buffer, e.g.:

int someInt = readBuffer.getInt();
long someLong = readBuffer.getLong();

这篇关于网络字节顺序到java中的主机字节顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆