I've been debugging some more dynamically generated parsers. After learning about the differences of bit8 and uint8 from issue #239 I switched some of uint8 to bit8 for sections where I'm not sure everything is byte aligned. I found some very unexpected behavior. See example below:
import { Parser } from "binary-parser";
const body = Buffer.from("082F00000682A20A70050E32", "hex");
const incorrect = new Parser()
.endianness("big")
.bit8("RATE")
.bit8("FIELD")
.nest("TIME", {
type: new Parser().uint32("coarse").bit24("fine")
})
// Result { TIME: { coarse: 137297920, fine: 10619504 }, RATE: 6, FIELD: 130 }
const correct = new Parser()
.endianness("big")
.uint8("RATE")
.uint8("FIELD")
.nest("TIME", {
type: new Parser().uint32("coarse").bit24("fine")
})
// Result { RATE: 8, FIELD: 47, TIME: { coarse: 1666, fine: 10619504 } }
Looking at the parser code you can see that the nested code is output before the bits.
I've been debugging some more dynamically generated parsers. After learning about the differences of bit8 and uint8 from issue #239 I switched some of uint8 to bit8 for sections where I'm not sure everything is byte aligned. I found some very unexpected behavior. See example below:
Looking at the parser code you can see that the nested code is output before the bits.
Incorrect:
Correct: