Why is creating a Float32Array with an offset that isn't a multiple of the element size not allowed?

后端 未结 3 1736
伪装坚强ぢ
伪装坚强ぢ 2020-12-15 22:35

I\'d like to read a binary file with a few 32 bit float values at byte offset 31.

Unfortunately, new Float32Array(buffer, 31, 6); does not work. An offs

3条回答
  •  無奈伤痛
    2020-12-15 22:58

    You can use slice to get a new ArrayBuffer whose contents are a copy of this ArrayBuffer's bytes from begin, inclusive, up to end

    const buffer = new ArrayBuffer(250);
    const list = buffer.slice(10); // index [11,250]
    const nums = new Int32Array(list, 0, 60);
    

提交回复
热议问题