-
Notifications
You must be signed in to change notification settings - Fork 34
ByteOrdering
There are two byte ordering modes known in the modern digital world
- Big-endian - the most significant byte of a word is stored in the smallest address, it is the default mode for Java and it is used in JBBP by default
- Little-endian - the least significant byte is stored in the smallest address
JBBP supports both the modes for parsing and packing
It is impossible to define the byte ordering mode for all parsing fields because JBBP uses the big-endian mode by default for parsing, but it is possible to define a byte ordering mode per a field with the special modifier < (little-endian) or > (big-endian) which should be placed before the field type (but it doesn't work for structures, only for primitive types and primitive type arrays!)
JBBPParser.prepare("<byte littleEndianField; byte bigEndianField;");
in the example above, the littleEndianField will be parsed in the little endian byte order but the bigEndianField will be parsed in the default big-endian order.
In opposite of parsing, you can define the byte order for all operations of packing
JBBPOut.BeginBin(JBBPByteOrder.LITTLE_ENDIAN).Int(0,0).Short(21,96).Int(512).End().toByteArray();
but also it is possible to change the byte order during packing with the ByteOrder?() command, it makes effect for all next multi-byte commands of the packing object
JBBPOut.BeginBin().ByteOrder(JBBPByteOrder.BIG_ENDIAN).Int(512).ByteOrder(JBBPByteOrder.LITTLE_ENDIAN).Int(512).End().toByteArray();