binary-parser icon indicating copy to clipboard operation
binary-parser copied to clipboard

[Question] How to read a 33 bits field?

Open enricovittorini opened this issue 2 years ago • 1 comments

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

enricovittorini avatar Jun 07 '22 06:06 enricovittorini

can someone help?

enricovittorini avatar Jul 05 '22 04:07 enricovittorini

hi! is this a live project?

enricovittorini avatar Sep 03 '22 05:09 enricovittorini

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.

keichi avatar Sep 04 '22 12:09 keichi

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

enricovittorini avatar Sep 04 '22 12:09 enricovittorini

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.

keichi avatar Sep 05 '22 03:09 keichi

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

keichi avatar Sep 09 '22 01:09 keichi