Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C Node.JS在不均匀偏移处反序列化浮动_C_Node.js_Embedded_Systems Programming - Fatal编程技术网

C Node.JS在不均匀偏移处反序列化浮动

C Node.JS在不均匀偏移处反序列化浮动,c,node.js,embedded,systems-programming,C,Node.js,Embedded,Systems Programming,这个问题与我以前的问题有关: 我通过蓝牙LE将数据传输到node.js服务器。以下是序列化数据的格式: top: 10 bit (unsigned integer) bottom: 10 bit (unsigned integer) accelerometerX: 23 bit (truncated 32-bit floating point) accelerometerY: 23 bit ... 数据作为node.js缓冲区接收。我能够解析top和bottom,因为它们是整数,我可以对它们

这个问题与我以前的问题有关:

我通过蓝牙LE将数据传输到node.js服务器。以下是序列化数据的格式:

top: 10 bit (unsigned integer)
bottom: 10 bit (unsigned integer)
accelerometerX: 23 bit (truncated 32-bit floating point)
accelerometerY: 23 bit
...
数据作为node.js缓冲区接收。我能够解析top和bottom,因为它们是整数,我可以对它们使用shift操作

top = data.readUInt16LE() & 0x3FF;
bottom = data.readUInt16LE(1);
bottom = (bottom >> 2) & 0x3FF;

accX = data.readUInt32LE(2);
accX = ((accX >> 4) & 0x7FFFFFF) << 9;
top=data.readUInt16LE()&0x3FF;
底部=数据读数16le(1);
底部=(底部>>2)&0x3FF;
accX=data.readUInt32LE(2);

accX=((accX>>4)&0x7FFFFFF)这不是一个很好的解决方案,但您可以使用另一个缓冲区进行这种转换:

function intToFloat(integer) {
    let buf = Buffer.alloc(4);
    buf.writeUInt32LE(integer);
    return buf.readFloatLE();
}
编辑:

还有另一种解决方案,也适用于现代浏览器环境:

function intToFloat(integer) {
    let intArray = new Uint32Array([ integer ]);
    let floatArray = new Float32Array(intArray.buffer);
    return floatArray[0];
}

23
bits-mens
0x7FFFFF
。您有一个
F
太多了。