Skip to content Skip to sidebar Skip to footer

Nodejs Buffer Bitwise Slicing

I'm transmitting data through bluetooth LE from a chip to node.js server. Firmware code: uint16_t txBuf[5] top &= 0x3FF; bottom &= 0x3FF; txBuf[0] = top + (bottom <<

Solution 1:

Well you could use the new Uint1Array "JavaScript's missing TypedArray" which is basically a bit field, with the same API as every other Typed Array.

So in your case:

constUint1Array = require('uint1array');
const bits = newUint1Array( newUint8Array(txBuf).buffer );
const top = bits.slice(0,10);
const bottom = bits.slice(10,20);

Solution 2:

Not sure why you don't want to do bit manipulation. JavaScript can do bit manipulation fine. The C bit manipulation stuff might not even need to be changed, or only a little.

JavaScript typed arrays may speed things up a bit. Double check your Node version and see the Buffer docs. They have some methods like readUInt8 etc. that might help.

Also you can manipulate bits as string if its easier (and if its not too slow), then use parseInt('01010101',2) to convert to a number. Also .toString(2) to convert to binary.

Post a Comment for "Nodejs Buffer Bitwise Slicing"