In Node.js, I was trying to cast a buffer which contains Int16Array buffer elements like below.
I have a list of numbers like following 255,256,257,258
Int16Array(4) [ 255, 256, 257, 258 ]
<Buffer ff 00 00 01 01 01 02 01>
So when I have a buffer like <Buffer ff 00 00 01 01 01 02 01>
what should I do to return back to the original array as 255,256,257,258
var ar = new Int16Array([255, 256, 257, 258])
var buf = Buffer.from(ar.buffer)
console.log('Here we have this', buf)
> <Buffer ff 00 00 01 01 01 02 01>
//And I need to cast back to array as [255,256,257,258]
Just as the comment says, typed arrays have constructor creating a view of an existing buffer:
Ok, sorry, I missed the Node.js part. So, according to
Buffer
, aBuffer
is anUint8Array
for most purposes: