keichi/binary-parser

[Question] How to read a 33 bits field?

enricovittorini opened this issue · 6 comments

Hello all,
Can someone guide me on how to read 33 bits? The bitX() is limited to 32
Thanks!

can someone help?

hi! is this a live project?

Apologies, I missed this one. Currently, there are no built-in methods to read a 33-bit field. A workaround would be to combine a 32-bit field and a 1-bit field manually.

Thanks @keichi would you show me an example how to do it?
Thanks

Here's an example:

const Parser = require("binary-parser").Parser;

const parser = new Parser()
    .bit32("a")
    .bit1("b");

const buf = Buffer.from("aaaaaaaa80", "hex");
const res = parser.parse(buf)

console.log((BigInt(res.a) << BigInt(1) | BigInt(res.b)).toString(16)); // -> 155555555

Note that you need to use BigInt since bitwise operations on Number automatically convert the operands and results to 32-bit integers.

I will close this issue for now. Please reopen if needed.