0

I want to read a binary file of float32 with javascript (written by python). I get the wrong values:

0,-428443616,-6.332091191811711e-23,-429492128,4.600602988224807e-41,0,0,0,4.618539608568165e-41,1.793662034335766e-43,-428443616,4.17232...

instead of right values:

0.00000000 0.80000001 -0.6000000 0.10000000...

code:

var mRequest = new XMLHttpRequest();
var url="https://storage.googleapis.com/crackling-torch-8396.appspot.com/ac395516-96d0-4533-8e0e-efbc681902ee/studies/-KgPah7nxgHsVB26Nlaf/meshes/-KgPayhUOPYlMx12mn7m/shapes/GEOMFACE1_n.bin";

mRequest.open('GET', url);
mRequest.responseType = 'arraybuffer';

mRequest.onreadystatechange = function () {
    if (mRequest.readyState === 4) {
        // Get bytes
        var buffer = mRequest.response;
        var dataview = new DataView(buffer);
        // Create buffer (4 bytes / float)
        var mFloatArray = new Float32Array(buffer.byteLength / 4);
        // Copy floats
        for (var i = 0; i < mFloatArray.length; i++) 
        {
            mFloatArray[i] = dataview.getFloat32(i * 4); // At every 4th byte
        }
        console.log("mFloatArray "+mFloatArray);
        // Do something with mFloatArray
    }
};

mRequest.send();
2
  • Expected result is logged at console here Commented Mar 29, 2017 at 16:19
  • I edited the question to make more clear which values are wrong Commented Mar 29, 2017 at 16:39

1 Answer 1

2

Just tested, it is a matter of little-endian order of the bytes. it works for me, with a little modification, parameter true for little-endian:

mFloatArray[i] = dataview.getFloat32(i * 4, true); // At every 4th byte
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.