Let's say I have an Uint8Array of 4096 items where each of them is the value 0xff or 255. I want to convert this array into a Float64Array where 8 values of the previous array correspond to a one 64-bit float. I have tried this code:
// Every item is 1 byte in pixels array, I want 8 of them to represent
// a 64-bit float in the float64Array
let pixels = new Uint8Array([255, 255, 255 ... , 255]); // 4096 values
let floatArr = new Float64Array(pixels.buffer); // Should be 512 values
console.log(floatArr); // This shows NaNs all over.
I have seen couple more questions that seem to be close what I am asking, but I could not get the idea from the answers provided to them. After inspecting Mozilla documentation, this seemed to me the correct way of using, however, I am not getting the expected result. How can I make this happen? Also, I am limited to the browser, you can assume the latest versions.
Edit: The expected result for a sample run is:
// pixels array is the input
let pixels = new Uint8Array([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]); // 16x 0xff, 16 bytes
let floatArr = new Float64Array(pixels.buffer) // I expect this to be
// [0xffffffffffffffff, 0xffffffffffffffff] which is 8bytes x 2,
// Float64Array [ 18446744073709552000, 18446744073709552000 ]
BigUint64ArrayFloat64. Essentially the code you have is equivalent to C++uint8_t pixels[] = ...; auto floatArr = reinterpret_cast<double[]>(pixels);.