本文介绍了Nodejs缓冲按位切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过蓝牙LE从芯片向node.js服务器传输数据。

I'm transmitting data through bluetooth LE from a chip to node.js server.

固件代码:

uint16_t txBuf[5]
top &= 0x3FF;
bottom &= 0x3FF;
txBuf[0] = top + (bottom << 10);
txBuf[1] = bottom >> 2;

基本上,前10位是顶部,下一位是底部。我可以在node.js中打印
缓冲区:

Basically, the first 10 bit is top, and the next 10 bit is bottom. I could printthe 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

如何在不进行位操作的情况下在javascript和node.js中解析此问题?

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

推荐答案

不确定为什么你不想做位操作。 JavaScript可以很好地进行操作。 C位操作的东西可能甚至不需要更改,或者只需要一点点。

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类型的数组可能会加快速度。仔细检查您的Node版本并查看Buffer文档。他们有一些像readUInt8等可能有帮助的方法。

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.

如果它更容易(如果它不是太慢)你也可以将位操作为字符串,然后使用 parseInt('01010101',2)转换为数字。还要 .toString(2)转换为二进制文件。

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.

这篇关于Nodejs缓冲按位切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 05:07