4

I have an ArrayBuffer that I convert to a Uint8Array so that I can use traditional array access with square brackets and gather a subarray. Now that I have the correct set of 4 bytes that describe the 32-bit (little endian) floating point number, I don't seem to have an easy way to convert to the floating point value:

var startIndex = 2;
var buffer = new Uint8Array(data)
buffer.subarray(startIndex, startIndex + 4);
var myNumber = ?uint8ArrayToFloat(buffer);
console.log(myNumber);

I'm new to JavaScript and am still looking around different docs...

tarabyte
  • 17,837
  • 15
  • 76
  • 117

1 Answers1

5

You can use DataView.getFloat32. First, you would create the DataView from the original ArrayBuffer (or the Uint8Array). getFloat32 takes an optional parameter that allows you to specify the endianess of the data you are reading.

Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
  • 1
    how can you create a `DataView` from a `Uint8Array`? I'm getting the error that my first argument to the `DataView` constructor must be an `ArrayBuffer` – tarabyte Oct 06 '14 at 23:31
  • ah, you have to realize when you're going against the flow sometimes. refactored everything to pass around array buffers instead. – tarabyte Oct 07 '14 at 00:02