0

I transmit data through bluetooth to a nodejs buffer. I need to get the first 23 bits, append 9 least significant bits of 0, and parse it as an IEEE 32-bit floating points. The following code use the buffer 0x5F80, which should return the correct result of -0.5.

qY = data.readUInt32LE();
console.log(qY)
qY = (qY & 0x7FFFFF) << 9;    // get first 23 bits, and append 9 LSB of 0.
console.log(qY);              // should be parsed as unsigned int, not signed int.

let buf = Buffer.alloc(4);
buf.writeUInt32LE(qY);   //error here as qY is negative, index out of range
return buf.readFloatLE();

The result:

6258688
-1090519040

How could I fix this problem? The code works fine with buffer holding positive truncated floats, such as 0.5.

Rob
  • 14,746
  • 28
  • 47
  • 65
Dzung Nguyen
  • 3,794
  • 9
  • 48
  • 86

0 Answers0