//方法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);