简体   繁体   English

将浮点值写入节点缓冲区

[英]Write float value in Node buffer

I am reading sensor data and I need to send these data via bluetooth so I am using noble/bleno library to subscribe data every time the value change. 我正在读取传感器数据,因此需要通过蓝牙发送这些数据,因此每次值更改时,我都使用noble / bleno库来订阅数据。 Here I am confusing how to send data as a buffer. 在这里,我很困惑如何将数据作为缓冲区发送。

I have data something like value = 24.3756 我有类似值= 24.3756的数据

So I need to write this in buffer: 所以我需要在缓冲区中写这个:

let buf = new Buffer(2);
buf.writeIntLE(buf);

But when I convert to ArrayBuffer value shows only 24 (not getting after decimal point) 但是当我转换为ArrayBuffer时,值仅显示24(小数点后没有得到)

How to send full float value and read as array buffer ? 如何发送完整的浮点值并读取为数组缓冲区?

First of all please read Buffer documentation here . 首先,请在此处阅读Buffer文档。

Then please don't use deprecated functions. 然后,请不要使用不推荐使用的功能。 If you need to create a new buffer use Buffer.alloc or Buffer.allocUnsafe . 如果需要创建新的缓冲区,请使用Buffer.allocBuffer.allocUnsafe When you're creating buffer ensure that it can hold the data you want to write there. 在创建缓冲区时,请确保它可以保存您要在其中写入的数据。 Next please use a suitable method for writing data. 接下来,请使用合适的方法写入数据。 You're writing floating point number, then you have to use Buffer.writeFloatBE/Buffer.writeFloatLE . 您正在写浮点数,然后必须使用Buffer.writeFloatBE/Buffer.writeFloatLE If you do everything I mentioned you'll end up with a correct solution: 如果您做了我提到的所有事情,最终将得到正确的解决方案:

const buffer = Buffer.allocUnsafe(4);
buffer.writeFloatLE(24.3756);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM