0

I have to read a binary file which is said to be encoded 4 byte single format and never having to work with binary data, I don't know what this means.

I can do this reading a file with binary data in JavaScript:

 d = new FileReader();
 d.onload = function (e) {
   var i, len;
   // grab a "chunk"
   response_buffer = e.target.result.slice(0, 1024);
   view = new DataView(response_buffer);

   for (i = 0, len = response_buffer.byteLength; i < len; i += 1) {
      // hmhm
      console.log(view.getUint8(i));
   }
}
d.readAsArrayBuffer(some_file);

Which runs a loop from 0 to 1023 and I am getting numbers on the console, but I don't know if this is my decoded data :-)

Question:
What is 4 byte single format and how do I access the data correctly? What is the difference between say getUint8() and getint8() or getInt32() in "human understandable language"?

Thanks!

frequent
  • 27,643
  • 59
  • 181
  • 333
  • `4 byte single format` is not a commonly understood term in computer science. If you could expect your file to be a series of single precision floating point numbers, then I might guess that `4 byte single format` means single precision floating point because each of those is four bytes long. – jfriend00 May 24 '14 at 12:45
  • I just found this on [wikipedia](http://en.wikipedia.org/wiki/Single-precision_floating-point_format) which confirms your guess. So now I'm using `getFloat32(i)`, which seems to return correct values (what I was hoping to read), but my counter fails at 1021 (of 1024). Is this `signed/unsigned` difference? If you put your guess in an answer, I will check. – frequent May 24 '14 at 12:48
  • 1
    `getUint8()`, `getint8()` and `getInt32()` are requests to get a certain number of bits from an ArrayBuffer and then interpret them as a signed or unsigned integer. A signed integer will use one particular bit to represent the sign of the number, an unsigned integer will use that same bit to represent a larger range of numbers, the sign can't be negative on an unsigned number. You are in essence telling your script how to parse the binary data and turn it into something meaningful. – jfriend00 May 24 '14 at 12:48

1 Answers1

1

4 byte single format is not a commonly understood term in computer science.

If you could expect your file to be a series of single precision floating point numbers, then I might guess that "4 byte single format" means single precision floating point because each of those is four bytes long.

You will want to use getFloat32() to parse single precision floating point numbers from the binary stream.


If you want 1024 numbers parsed with getFloat32(), then you need 1024*4 bytes and you need to advance your for loop by four bytes each time since getFloat32() processes four bytes at a time:

d = new FileReader();
 d.onload = function (e) {
   var i, len;
   // grab a "chunk"
   response_buffer = e.target.result.slice(0, 1024 * 4);
   view = new DataView(response_buffer);

   for (i = 0, len = response_buffer.byteLength; i < len; i += 4) {
      // hmhm
      console.log(view.getFloat32(i));
   }
}
d.readAsArrayBuffer(some_file);

Also, please note that IE10 and IOS 5 do not have the .slice() method for an ArrayBuffer if you're planning on using this in a general web page.

jfriend00
  • 683,504
  • 96
  • 985
  • 979