How to properly access the elements in data views based on the data type?
Consider the following example code:
const data = new Uint16Array([7, 12, 100]);
const view = new DataView(data.buffer);
console.log(view.getUint16(0), view.getUint16(1), view.getUint16(3));
The output is 1792 12 100
. The first element is wrong, and the other element offsets are not multiples of 2 as 16 bit types would suggest. Using view.getUint8(0)
does output 7
.
What is the correct way to access elements in a data view using .getTYPE()
?
The real use-case is a binary file that is read in as an ArrayBuffer
of mixed sized values.