简体   繁体   English

如何从 .NET 的 BinaryWriter 序列化的四个字节中读取单精度浮点数

[英]How to read a single-precision floating-point number from four bytes serialized by .NET's BinaryWriter


So, I'm trying to build a class to read data from a buffer just like .NET's BinaryReader would do. 所以,我正在尝试构建一个类来从缓冲区读取数据,就像 .NET 的 BinaryReader 会做的那样。 This is to read Celeste's maps format (which is stored in binary) in a map editor app I'm building using Electron (so I'm implicitly using Node.JS & JavaScript). 这是在我使用 Electron 构建的地图编辑器应用程序中读取 Celeste 的地图格式(以二进制形式存储)(因此我隐式使用 Node.JS 和 JavaScript)。
I coded all the data readers necessary using the .NET reference source but I can't figure out how to read a single-precision floating-point number, which is stored as four bytes. 我使用 .NET 参考源对所有必需的数据读取器进行了编码,但我无法弄清楚如何读取存储为四个字节的单精度浮点数。
In the reference source, there's some code that acquires the memory address of the four bytes as a unsigned 4-bytes integer then reads that address as a single-precision float, that works for .NET but is impossible to do in JavaScript (or Node.JS). 在参考源中,有一些代码将四个字节的内存地址作为无符号的 4 字节整数获取,然后将该地址作为单精度浮点数读取,这适用于 .NET,但在 JavaScript(或 Node .JS)。
My current class code can be found on Hastebin , and I'm so sorry if I didn't provide enough information since this is my first question, or if I mark an answer correct very late. 我目前的班级代码可以在Hastebin找到,如果我没有提供足够的信息,我很抱歉,因为这是我的第一个问题,或者我很晚才将答案标记为正确。

This is possible with node.js.这可以通过 node.js 实现。 Taken straight from the documentation :直接取自文档

var buf = new Buffer(4);
 
buf[0] = 0x00;
buf[1] = 0x00;
buf[2] = 0x80;
buf[3] = 0x3f;
 
console.log(buf.readFloatLE(0));
 
// 0x01

where LE refers to little-endian.其中LE指的是小端。 There is a corresponding readFloatBE for big-endian. big-endian 有一个对应的readFloatBE

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

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