Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Uint8Array to a float in javascript?

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...

like image 219
tarabyte Avatar asked Oct 26 '25 05:10

tarabyte


1 Answers

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.

like image 99
Alexander Gessler Avatar answered Oct 28 '25 20:10

Alexander Gessler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!