具有偏移量的JavaScript数组的节点缓冲区

时间:2018-09-28 02:15:32

标签: javascript node.js

我正在尝试将节点Buffer转换为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 个字节的偏移量开头的数组?

1 个答案:

答案 0 :(得分:1)

只需使用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.