ByteBuffer Little Endian insert not working

后端 未结 2 1556
隐瞒了意图╮
隐瞒了意图╮ 2021-02-18 17:11

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 rec

2条回答
  •  长发绾君心
    2021-02-18 17:43

    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));
    

    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));
    

    Prints [88, 0, 0, 0]

提交回复
热议问题