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

int24 and uint24 support

Open South-Paw opened this issue 6 years ago • 3 comments

Is it possible for the package to add support for .int24() and .uint24()?

Since you can't sequence more than 4 bytes using .bit24() I'm in a bit of a pickle with a buffer I'd like to decode.

  • #83 is similar on 'why no unit24 support'
  • #90 as an issue with sequencing bits, but also looks like they could use a .int24()

If this isn't possible, can someone direct me to a package that is similar to this but supports int24()?

Thanks 👍

South-Paw avatar Mar 23 '19 04:03 South-Paw

I will add support for int24() and uint24(). Can you use the following workaround for now?

var parser = new Parser().array("uint24", {
  type: "uint8",
  length: 3,
  formatter: function(arr) {
    return arr[2] << 16 | arr[1] << 8 | arr[0];
  }
});

var parser = new Parser().array("int24", {
  type: "uint8",
  length: 3,
  formatter: function(arr) {
    var val = arr[2] << 16 | arr[1] << 8 | arr[0];

    return (val > 0x7fffff) ? 0xff000000 | val : val;
  }
});

keichi avatar Mar 23 '19 14:03 keichi

Aha! I had a feeling there'd be a way to work around it but just didn't know how to even start doing it with the library as this sort of thing isn't really my forte or area of expertise.

@keichi you're awesome, will use this till its in the package - thanks heaps!! ❤️ 👍

South-Paw avatar Mar 24 '19 03:03 South-Paw

Hi @keichi, just come across this again and wondering if the int24 and uint24 will be added to the package sometime soon?

Looks like I also have a use case for int24be and uint24be now as the workarounds you provided don't seem to work for one of my requirements - I can only get the correct expected value out of the buffer if I use the following:

// <Buffer f9 59 a6 1a 57 c8 0f d3 00 48 01 a9>

let a = buffer.readIntBE(0, 3);
let b = buffer.readIntBE(3, 3);

console.log({ a, b }); // { a: -435802, b: 1726408 }

I assume it doesn't work because these are big-edian int values? Is there another way to work around this using the library? In the mean time I'm just creating two objects and merging them before returning from my helper function 😅

South-Paw avatar Oct 12 '19 09:10 South-Paw