我正在通过蓝牙 LE 将数据从芯片传输到 node.js 服务器。
固件代码:
uint16_t txBuf[5]
top &= 0x3FF;
bottom &= 0x3FF;
txBuf[0] = top + (bottom << 10);
txBuf[1] = bottom >> 2;
基本上,前 10 位是顶部,接下来的 10 位是底部。我可以打印 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
如何在不进行位操作的情况下在 javascript 和 node.js 中解析它?
最佳答案
那么您可以使用新的 Uint1Array “JavaScript 缺少 TypedArray”,它基本上是一个位字段,具有与所有其他类型化数组相同的 API。
所以在你的情况下:
const Uint1Array = require('uint1array');
const bits = new Uint1Array( new Uint8Array(txBuf).buffer );
const top = bits.slice(0,10);
const bottom = bits.slice(10,20);
关于javascript - Nodejs缓冲区按位切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39238826/