我通过蓝牙将数据传输到nodejs缓冲区。我需要获得前23位,追加9个最低有效位0,并将其解析为IEEE 32位浮点。以下代码使用缓冲区0x5F80,它应返回-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();
结果:
6258688
-1090519040
我该如何解决这个问题?该代码适用于缓冲区保持正截断浮点数,例如0.5。