javascript - Nodejs缓冲区按位切片

标签 javascript c node.js

我正在通过蓝牙 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/

相关文章:

c - 在嵌入式 C 中使用 printf 进行多级调试

c - 内存和指针

javascript - 访问对象中的函数,嵌套在另一个函数中

Javascript OOP - 从同一类的不同方法调用方法

objective-c - 在 Objective C 中声明和使用 C 函数

javascript:如何将变量转换为具有相同名称并返回原始值的函数

javascript - 将 node.js 更新为 0.12 header 错误

node.js - 找出 Node 中哪个文件需要另一个文件

javascript - 如何使用 ExpressJS 将 JSON 作为 UTF-8 发送?

javascript - 当我在 Internet Explorer 中执行打开的 xhhtprequest 时,权限被拒绝消息