javascript - 带偏移量的 javascript 数组的 Node 缓冲区

标签 javascript node.js

我正在尝试将 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/

相关文章:

javascript - Selenium - 单击按钮直到出现某些元素

javascript - 从 ASIN 获取亚马逊商品图片和描述

javascript - Sails.js 查询模型按集合属性过滤

javascript - 复选框状态不切换。 Material 用户界面 react

javascript - 使用 Mongoose 避免回调 hell

javascript - 这两种功能/方法有什么区别?

node.js - MEAN 堆栈 - 依赖模板(Angular 与模板引擎)?

javascript - 如何使用knockoutjs在点击函数中传递固定参数?

javascript - ng-在 Angularjs 中重复多次

node.js - Bull队列: When a job fails,如何停止队列处理剩余作业?