nodejs 将Uint8Contents对象转数组
下面的输出的对象数据,nodejs如何转为普通数组
ArrayBuffer {
[Uint8Contents]: <4d 57 a3 05 b7 53 0c 6c fa d6 e6 ed 41 d4 c8 53 61 0a ae f4 5e 04 16 b9 35 db e8 2d 73 3d 24 b2>,
byteLength: 32
}
示例代码如下
var data = '4d 57 a3 05 b7 53 0c 6c fa d6 e6 ed 41 d4 c8 53 61 0a ae f4 5e 04 16 b9 35 db e8 2d 73 3d 24 b2'.split(' ').map(i => parseInt(i, 16));
var buffer = Buffer.from(data);
var array = new Uint8Array(buffer);
var arraybuffer = array.buffer;
console.log(arraybuffer)
var arr2 = Array.prototype.slice.call(new Uint8Array(arraybuffer));
console.log(arr2);
可以使用Node.js的Buffer模块,将Uint8Contents对象转换为Buffer对象,然后使用Buffer.toArrayBuffer()方法将Buffer对象转换为数组。
const buf = Buffer.from(Uint8Contents);
const arrayBuffer = buf.toArrayBuffer();
以下答案引用自GPT-3大模型,请合理使用:
栗子
可以使用ArrayBuffer.prototype.slice方法,将Uint8Contents对象转换为Uint8Array类型,然后再转换为数组。代码如下:
// 定义Uint8Contents对象
let uint8Contents = ArrayBuffer {
[Uint8Contents]: <4d 57 a3 05 b7 53 0c 6c fa d6 e6 ed 41 d4 c8 53 61 0a ae f4 5e 04 16 b9 35 db e8 2d 73 3d 24 b2>,
byteLength: 32
};
// 将Uint8Contents对象转换为Uint8Array类型
const uint8Array = new Uint8Array(uint8Contents);
// 将Uint8Array类型转换为数组
const array = [...uint8Array];
console.log(array); // 输出[77, 87, 163, 5, 183, 83, 12, 108, 250, 214, 230, 237, 65, 212, 200, 83, 97, 10, 174, 244, 94, 4, 22, 185, 53, 219, 232, 45, 115, 61, 36, 178]
如果我的回答解决了您的问题,请采纳我的回答
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!