read Float32Array from file

Posted on
  • Is there a better way to read data from a file into Float32Array than this ?

    var f = E.openFile("myTestF.idx","r");
    f.skip(4);
    var b = new Float32Array(E.toUint8Array(f.read(4)).b­uffer);
    var c = new Float32Array(E.toUint8Array(f.read(8)).b­uffer);
    f.close();
    
    
  • Do you know the length?

    What you're doing is fine, but I'd be tempted to just read the whole thing (or at least big chunks) into memory and use views to read it directly:

    var f = E.openFile("myTestF.idx","r");
    var bytes = E.toUint8Array(f.read(16));
    var floats = new Float32Array(bytes.buffer);
    f.close();
    

    By using .buffer you're not actually allocating any more memory - the two arrays are sharing the same data.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

read Float32Array from file

Posted by Avatar for JumJum @JumJum

Actions