ByteBuffer的小端插入不工作 [英] ByteBuffer Little Endian insert not working

查看:701
本文介绍了ByteBuffer的小端插入不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须做出一个遗留系统和Android设备之间的双向通信。 遗留系统采用little endian字节顺序。 我已经成功地实现了接收部分,但不发作品。

I have to make a two way communication between a legacy system and an android device. The legacy system uses little endian byte ordering. I have successfully implemented the receiving part, however sending not works.

奇怪,因为对我来说似乎的ByteBuffer类故障(我很难相信这一点)

Strange because for me it seems that the ByteBuffer class malfunctions (I can hardly believe that)

    ByteBuffer byteBuffer = ByteBuffer.allocate(4);
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    byteBuffer = ByteBuffer.allocate(4);
    byteBuffer.putInt(88);
    byte[] result = byteBuffer.array();

结果: [0,0,0,88]

Results: [0, 0, 0, 88]

    ByteBuffer byteBuffer = ByteBuffer.allocate(4);
    byteBuffer.order(ByteOrder.BIG_ENDIAN);
    byteBuffer = ByteBuffer.allocate(4);
    byteBuffer.putInt(88);
    byte[] result = byteBuffer.array();

也导致相同的: [0,0,0,88]

Also results the same: [0, 0, 0, 88]

但是,如果我没有记错小尾数排序应该返回: [88,0,0,0]

However if I'm not mistaken little endian ordering should return: [88, 0, 0, 0]

那么,什么是我错过了点?

So what's the point I'm missing?

推荐答案

您由于某种奇怪的原因,重新初始化您的字节的缓冲区和扔掉,你改变了endian顺序的previous副本。下面code工作得很好,对我来说:

You are, for some strange reason, reinitializing your byte buffers and throwing away the previous copies where you had changed the endian order. The following code works just fine for me:

ByteBuffer byteBuffer = ByteBuffer.allocate(4);
byteBuffer.order(ByteOrder.BIG_ENDIAN);
byteBuffer.putInt(88);
byte[] result = byteBuffer.array();
System.out.println(Arrays.toString(result));

打印[0,0,0,88]

Prints [0, 0, 0, 88]

ByteBuffer byteBuffer = ByteBuffer.allocate(4);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
byteBuffer.putInt(88);
byte[] result = byteBuffer.array();
System.out.println(Arrays.toString(result));

打印[88,0,0,0]

Prints [88, 0, 0, 0]

这篇关于ByteBuffer的小端插入不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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