Preon decode and then encode doesn't return the same byte array

懵懂的女人 提交于 2019-12-08 07:46:56

问题


I am using Preon to encode enum fields in a byte array.

The fields of the first byte (example-byte: 0xf7=1111 01 11) are set up as this:

@BoundNumber(size="2") //Corresponds to 11
private byte b1_1_notUsed;

@BoundNumber(size="2") //Corresponds to 01
private ParkingBrakeSwitch parkingBrakeSwitch;

@BoundNumber(size="4")
private byte b1_3_notUsed; //Corresponds to 1111

Decoding works fine, thus I expect encoding the decoded object back to a byte array will yield the same byte array that I had from the beginning. This is not the case however. Instead the bits in the first byte are encoded like this:

0xdf=11 01 1111

That is, the segments are encoded in the reverse order! Why doesn't decode-encode return the same byte array? What do I have to do? Is there some kind of order annotation that I am missing?

Thankful for reply, because Preon documentation seems scarce!


回答1:


Question resolved.

The describing class did not take into account the byte order. Foolishly I did not consider the byte-order to be an issue within the bits in the byte itself. But this is obviously the case:

Example-byte: 0xf7=1111 01 11 (Big Endian: MSB first)

@BoundNumber(size="4", byteOrder=ByteOrder.BigEndian) //Corresponds to 1111
private byte b1_3_notUsed;

@BoundNumber(size="2", byteOrder=ByteOrder.BigEndian) //Corresponds to 01
private ParkingBrakeSwitch parkingBrakeSwitch;

@BoundNumber(size="2", byteOrder=ByteOrder.BigEndian) //Corresponds to 11
private byte b1_1_notUsed;

Now encoding returns 0xf7=1111 01 11 again when it has been re-encoded.



来源:https://stackoverflow.com/questions/20842635/preon-decode-and-then-encode-doesnt-return-the-same-byte-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!