It's not very clear what you're asking; or, rather, what it appears you're asking is a thing that makes no sense.
A Float32Array
instance is a view onto a buffer of "raw" byte values, like all typed arrays. Each element of the array represents 4 of those raw bytes. Extracting a value via a simple array lookup:
var n = float32array[1];
implicitly interprets those 4 bytes as an IEEE 32-bit floating point value, and then that value is converted to a standard JavaScript number. JavaScript numbers are always 64-bit IEEE floating point values.
Similarly, a Uint8Array
is a view onto a buffer, and each element gives the unsigned integer value of one byte. That is,
var n = uint8array[1];
accesses that element, interprets it as an unsigned one-byte integer, and converts that to a JavaScript number.
So: if you want to examine a list of 32-bit floating point values as the raw integer value of each byte, you can create a Uint8Array
that "shares" the same buffer as the Float32Array
:
var uintShared = new Uint8Array(float32array.buffer);
The number values you'd see from looking at the Uint8Array
values will not appear to have anything to do with the number values you get from looking at the Float32Array
elements, which is to be expected.
On the other hand, if you want to create a new Uint8Array
to hold the apparent values from the Float32Array
, you can just create a new array of the same length and copy each value:
var uintCopy = new Uint8Array(float32array.length);
for (let i = 0; i < float32array.length; ++i)
uintCopy[i] = float32array[i]; // deeply problematic; see below
Now that won't work too well, in general, because the numeric range of values in a Float32Array
is vastly greater than that of values in the Uint8Array
. For one thing, the 32-bit floating point values can be negative. What's more, even if you know that the floating point values are all integers in the range 0 to 255, you definitely will not get the same bit patterns in the Uint8Array
, for the simple reason that a 32-bit floating point number is just not the same as an 8-bit unsigned integer. To "preserve the IEEE-754 representation" makes no sense.
So that's the reality of the situation. If you were to explain why you think you want to somehow cram all 32 bits of a 32-bit IEEE float into an 8-bit unsigned integer, it would be possible to provide a more directly helpful answer.