有什么方法可以使用 Buffer javascript 包从字节中获取位级信息吗?

Is there any way to get Bit level info from Bytes using Buffer javascript package?

const session = {"data": [0, 91, 56, 110, 221, 112, 0, 0, 4, 1, 0, 0, 60, 2, 34, 255, 255, 255, 167, 237], "type": "Buffer"}

const buffer = new Buffer(session)
const buffer1 = buffer.subarray(14, 15)
console.log("buffer1",buffer1) //output : 34

我想得到如图所示的比特级数据。

这里是位级数据的详细信息

因为你有感兴趣的八位字节,所以你可以对其进行按位运算以获得感兴趣的位。这可以通过一个小函数来简化。

例如:

var Buffer = require('buffer/').Buffer;
const session = {"data": [0, 91, 56, 110, 221, 112, 0, 0, 4, 1, 0, 0, 60, 2, 34, 255, 255, 255, 167, 237], "type": "Buffer"};
const buffer = new Buffer(session);
const data = buffer.readUInt16LE(13);
console.log("Data", data);

function bits(value, bitsStart, bitsWidth){
  return (value >>> bitsStart) & ( Math.pow(2, bitsWidth) - 1);
}

console.log('Binary value:', (data).toString(2).padStart(16, '0'))
console.log('Session type:', bits(data, 0x00, 0x04))
console.log('Aborted:', bits(data, 0x04, 0x01))
console.log('Goal Complete:', bits(data, 0x05, 0x01))
console.log('Goal Extended:', bits(data, 0x06, 0x01))
console.log('Auto Letdown:', bits(data, 0x07, 0x01))
console.log('Leak Detected:', bits(data, 0x08, 0x01))

给出输出:

Data 8706
Binary value: 0010001000000010
Session type: 2
Aborted: 0
Goal Complete: 0
Goal Extended: 0
Auto Letdown: 0
Leak Detected: 0