Buffer、ArrayBuffer互转

//方法1
//
Buffer转ArrayBuffer function toArrayBufferbuf) { var ab = new ArrayBufferbuf.length); var view = new Uint8Arrayab); for var i = 0; i < buf.length; ++i) { view[i] = buf[i]; } return ab; } //ArrayBuffer转Buffer function toBufferab) { var buf = new Bufferab.byteLength); var view = new Uint8Arrayab); for var i = 0; i < buf.length; ++i) { buf[i] = view[i]; } return buf; }
//方法2
// Buffer转ArrayBuffer
const buf = Buffer.from"this is a test");
console.logbuf);
console.logbuf.toString));
var arrayBuf = buf.buffer; //Buffer的buffer属性返回arrayBuf
console.logarrayBuf);

//ArrayBuffer转Buffer
var arrayBuffer = new ArrayBuffer16);
const buffer = Buffer.fromarrayBuffer);
console.logbuffer.buffer === arrayBuffer);

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注