Skip to content
Advertisement

Reading floats from binary file javascript

I’m trying to read floats in javascript from a binary file that is created using Java.

The file is created in Java using DataOutputStream:

JavaScript

The file is retreived by http request and read like this:

JavaScript

dataLoaded function:

JavaScript

Output:

JavaScript

Expecting:

JavaScript

The file is sent with php:

JavaScript

It seems that somewhere there is a flaw in the conversion but I can’t figure out where.

UPDATE:

The problem was just as Sean Van Gorder stated, a difference in endianness. To work around this I used DataView to read the arrayBuffer (since the file will be read both in java and in javascript this was the best sulotion)

JavaScript

Output:

JavaScript

Advertisement

Answer

You’ve got a byte order mismatch. DataOutputStream writes in big-endian, but Float32Array usually reads in little-endian (depending on hardware). You’ll have to change the Java side or the Javascript side to match.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement