将对象值打包成 Javascript 中的 4 字节无符号整数

Pack object values into a 4 bytes unsigned integer in Javascript

位运算符是我的弱项。我想将 javascript 中的某些对象值打包/编码为 4 字节无符号整数(32 位)。 我找不到任何关于将给定值设置为字节的示例。

const obj = {
  line1: 1, // 1 bit
  line2: 1, // 1 bit
  volt: 15, // 4 bits
  temp_cur: 35, // 8 bits
  temp_min: 32, // 8 bits
  temp_max: 32, // 8 bits
  reserve: 0 // 2 bits
};

const buffArr = new ArrayBuffer(4);
const dataView = new DataView(buffArr);

// Not sure how to initialize an unsigned 32bit int
let packed = dataView.setUint32(0, 0, true);

// Set the values
packed = packed << 1 | obj.line1;
packed = packed << 1 | obj.line2;
packed = packed << 4 | obj.volt;
packed = packed << 8 | obj.temp_cur;
packed = packed << 8 | obj.temp_min;
packed = packed << 8 | obj.temp_max;
packed = packed << 2 | obj.reserve;

console.log(packed);

看来我也需要屏蔽这个?因为我将它设置为一个无符号的 32 位整数,以及字节顺序。

您的打包例程真的很糟糕,使用 & 屏蔽值,使用 >>> 进行无符号左移。见下文

const obj = {
  line1: 1, // 1 bit
  line2: 1, // 1 bit
  volt: 15, // 4 bits
  temp_cur: 35, // 8 bits
  temp_min: 32, // 8 bits
  temp_max: 32, // 8 bits
  reserve: 0 // 2 bits
};

const buffArr = new ArrayBuffer(4);
const dataView = new DataView(buffArr);

// Not sure how to initialize an unsigned 32bit int
let packed = dataView.setUint32(0, 0, true);

packed =  0x1 & (obj.line1 << 0); // No shift
packed += 0x2 & (obj.line2 << 1); // shift 0 + 1 bits
packed += 0x3c & (obj.volt << 2); // shift 0 + 1 + 1 bits
packed += 0x3FC0 & (obj.temp_cur << 6); // shift 0 + 1 + 1 + 4 bits
packed += 0x3FC000 & (obj.temp_min << 14); // shift 0 + 1 + 1 + 4 + 8 bits
packed += 0x3FC00000 & (obj.temp_max << 22); // shift 0 + 1 + 1 + 4 + 8 + 8 bits
packed += 0xc0000000 & (obj.reserve << 30); // shift 0 + 1 + 1 + 4 + 8 + 8 bits

let unpack = {
  line1: (packed & 0x1) >>> 0,  // mask out the specific bits and shift back into place
  line2: (packed & 0x2) >>> 1, 
  volt: (packed & 0x3c) >>> 2,
  temp_cur: (packed & 0x3FC0) >>> 6,
  temp_min: (packed & 0x3FC000) >>> 14,
  temp_max: (packed & 0x3FC00000) >>> 22,
  reserve: (packed & 0xc0000000) >>> 30
};
console.log(packed);
console.log(unpack);