I'm transmitting data through bluetooth LE from a chip to node.js server.
uint16_t txBuf top &= 0x3FF; bottom &= 0x3FF; txBuf = top + (bottom << 10); txBuf = bottom >> 2;
Basically, the first 10 bit is top, and the next 10 bit is bottom. I could print the buffer in node.js:
console.log(buffer) console.log(buffer.data) <Buffer cd d3 8d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00> 20
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.
Read your unsigned int from the buffer using included operations (https://nodejs.org/api/buffer.html#buffer_buf_readuintle_offset_bytelength_noassert)
>>> is the non-sign extending shift, which will be important in this case if you don't to randomly get a bunch of 1s instead of 0s!
So in your case:
const Uint1Array = require('uint1array'); const bits = new Uint1Array( new Uint8Array(txBuf).buffer ); const top = bits.slice(0,10); const bottom = bits.slice(10,20);