I tried to call .map on Uint8Array and got a surprise.
const tt = new Uint8Array(1);
tt[0] = 0xFF;
const ttt = tt.map(x => x.toString(2));
console.log(ttt[0]);
console.log(tt[0].toString(2));
I expected both output to be '11111111', but first console.log prints the number 199. Why?
That's because Uint8Array.map returns a Uint8Array. All the strings you return, get parsed as uint8s again ... and thus you get 199 (11111111 & (2 ** 8 - 1)).
You might wanna do:
Array.from(tt).map(x => x.toString(2))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With