4

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 << 10);
txBuf[1] = 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

How can I parse this in javascript and node.js without doing bit manipulation?

1
  • 1
    Without doing bit manipulation (yourself or through some package), you can't. It’s not built into Buffer. Commented Aug 31, 2016 at 0:06

2 Answers 2

2

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:

const Uint1Array = require('uint1array');
const bits = new Uint1Array( new Uint8Array(txBuf).buffer );
const top = bits.slice(0,10);
const bottom = bits.slice(10,20);
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

I was wondering if it's possible to do data.readUInt16LE(10) where 10 is the bit-wise position, not bytes.
No, see docs for buffer. But just do same as C code to mask ten bits using & which is bitwise AND

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.