Search code examples
javascriptarraybuffer

Why Int16Array to ArrayBuffer convert back Int16Array, result is different?


I have an array of data (split from a buffer) Int16Array[326784] with values like [579, 578, 576, 574, 570, ...].

When I run b = new Int16Array(a.buffer, 0, a.length) the result is Int16Array[326784] but the values are different: [0, 0, ..., 0, 18756, 19779, 2, 0, 19541, 4, ...]

If I repeat the conversion: c = new Int16Array(b.buffer, 0, b.length) the result is same as b.

Why is the first conversion different?


Solution

  • It caused by misunderstand TypedArray.

    In the question, a = Int16Array(bufferA, x, y);, a is only a view of buffer, the a.buffer is bufferA.

    So b = new Int16Array(a.buffer, 0, a.length) is equal b = new Int16Array(bufferA, 0, a.length)