Buffer、ArrayBuffer、DataView互转(node.js)

1.Buffer转ArrayBuffer

// 实例一
const buf = Buffer.from("this is a test");
console.log(buf);
console.log(buf.toString());
var arrayBuf = buf.buffer; //
console.log(arrayBuf);

2.ArrayBuffer转Buffer

// 实例二
var arrayBuffer = new ArrayBuffer(16);
const buffer = Buffer.from(arrayBuffer);
console.log(buffer.buffer === arrayBuffer);

3.ArrayBuffer转DataView

// 实例一
var arrayBuffer1 = new ArrayBuffer(256);
var view = new DataView(arrayBuffer1);
view.setUint32(0,300);
view.setUint32(4,100);
console.log(view.getUint32(0));
console.log(view.getUint32(4));

4.DataView转ArrrayBuffer

var arrayBuffer2 = new ArrayBuffer(256);
var view2 = new DataView(arrayBuffer2);
var buf2 = view2.buffer;
console.log(buf2);

posted @ 2019-01-23 15:39  jadeshu  阅读(2415)  评论(0编辑  收藏  举报