image-js / iobuffer

Read and write binary data in ArrayBuffers
http://image-js.github.io/iobuffer/
MIT License
43 stars 6 forks source link

Reading variable length Int #49

Closed dderevjanik closed 2 years ago

dderevjanik commented 2 years ago

Hello, is it possible to read int with non-standard bytes length? e.g. 4

Similar to https://stackoverflow.com/questions/30911185/javascript-reading-3-bytes-buffer-as-an-integer

ghost commented 2 years ago

You can't (afaik) unless doing the trick in the post you link

var decimal = (buffer[0] << 16) + (buffer[1] << 8) + buffer[2];

You could get the buffer from the IOBuffer.buffer and then update the offset. That is of BE and invert it for LE.

lpatiny commented 2 years ago

I had a similar problem parsing uavcan packets

If the data are not too big and time is not super critical you can convert your data to a BigInteger and then use

https://github.com/lpatiny/uavcan/blob/master/src/data/parser/util/parseInt.js