我正在尝试将 Node 缓冲区转换为 Uint8ClampedArray,但我想丢弃前 8 个字节。我试过这样:
buf = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
arr = new Uint8ClampedArray(buf, 8, 8);
但是看起来好像忽略了偏移量,arr
包含所有 buf
。
如何将 buf
转换为从偏移 n 字节开始的数组?
最佳答案
只需使用Buffer.slice
:
> arr = new Uint8ClampedArray(buf.slice(8));
Uint8ClampedArray [ 9, 10, 11, 12, 13, 14, 15, 16 ]
顺便说一下,以这种方式构建您的 Buffer
已被弃用:
> buf = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
<Buffer 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10>
[DEP0005] DeprecationWarning: Buffer() is deprecated due to security and
usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or
Buffer.from() methods instead.
关于javascript - 带偏移量的 javascript 数组的 Node 缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52547376/