Node的Arraybuffer做大小端转换

Nodejs 的 ArrayBuffeer大小端序转换:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getUint32

// 创建 ArrayBuffer
const buffer = new ArrayBuffer(4);

// 赋初始值
var bytes = new Uint32Array(buffer)
bytes[0] = 0x05;
bytes[1] = 0x00;
bytes[2] = 0x00;
bytes[3] = 0x00;

// buffer和Dataview
const view = new DataView(buffer);

// 按小端序取出数据
console.log("小端序:"+view.getUint32(0, 1));

// 按大端序取出数据
console.log("大端序:"+ view.getUint32(0, 0));

分享