iiSkLz_
iiSkLz_

Reputation: 35

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. 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).
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.
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).
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.

Upvotes: 1

Views: 178

Answers (1)

John H
John H

Reputation: 14655

This is possible with 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. There is a corresponding readFloatBE for big-endian.

Upvotes: 1

Related Questions