> new Buffer(Uint16Array.from([255]))
<Buffer ff>
> new Buffer(Uint16Array.from([65535]))
<Buffer ff>
As far as I understand from node's Buffer documentation, this is not expected behavior, the 8 most significant bits in every element of any Uint16Array are just truncated because every element of Uint16Array is converted to an element of Buffer. But Buffer elements are 1 byte long, while Uint16Array's are 2 bytes long, so every element from the Uint16Array should be two Buffer elements.
Relevant documentation (from node):
const arr = new Uint16Array(2);
arr[0] = 5000;
arr[1] = 4000;
const buf = new Buffer(arr.buffer); // shares the memory with arr;
console.log(buf);
// Prints: <Buffer 88 13 a0 0f>
Thanks for looking into this issue :+1: I'll try testing on older versions of node to narrow this down if I have time.
> new Buffer(Uint16Array.from([65535]).buffer)
<Buffer ff ff>
What you want to use is the Uint16Array鈥檚 ArrayBuffer, not the array itself (compare the arr.buffer part of the doc excerpt).
Wow, I just tried that and it worked. My fault! Thanks for the quick answer! I guess I should have RTFM.
Most helpful comment
What you want to use is the
Uint16Array鈥檚ArrayBuffer, not the array itself (compare thearr.bufferpart of the doc excerpt).