Currently, when the binary file contains 64 Bit floating points, I do:
fs.open('doubles.bin', 'r', (err, fd) => {
if (err) reject(err);
const buf = new Buffer(size);
fs.read(fd, buf, 0, size, start, (err2) => {
if (err2) reject(err2);
const f32 = new Float32Array(buf.buffer);
const data = Array.prototype.slice.call(f32);
resolve({ id, data });
});
});
(the binary is a simple list of numbers. They are not separated by anything)
I think this works because the Float32Array view expects to read the standard JavaScript Numbers, which are 64 Bit in size.
As soon as the binary contains 32 Bit Numbers, i only get garbage out of it (NaNs and wrong Numbers).
How can I read a binary file, containing 32 Bit floating point numbers, with JavaScript and convert it to an Array?