简体   繁体   English

将字节数组转换为 320 位,然后位提取 16 位,然后 10 位

[英]conversion of Byte array to 320 bits and then bit extraction 16 bits then 10 bits

i am hoping some clever people here would be able to help me, i am new to JavaScript and trying to figure out how to code the following as i get stuck when the payload string is not standard 8 bit 1 byte groups, so i receive 320 bit 40 byte array.我希望这里的一些聪明人能够帮助我,我是 JavaScript 的新手,并试图弄清楚如何编写以下代码,因为当有效负载字符串不是标准的 8 位 1 字节组时我卡住了,所以我收到 320位 40 字节数组。 the first few values are fine to extract as they are 16 bits, then 8 bits then 32 bits of info but the next value is only 10 bits and i get stuck here.前几个值可以很好地提取,因为它们是 16 位,然后是 8 位,然后是 32 位信息,但下一个值只有 10 位,我被困在这里。 so i should probably be converting the 40 bytes into 320 bits and then shifting right for each number of bit per variable?所以我可能应该将 40 字节转换为 320 位,然后为每个变量的每个位数右移? i am not sure how to do this.我不知道该怎么做。 This is my payload format and in brackets is the number of bits for that variable.这是我的有效载荷格式,括号中是该变量的位数。 any help would be appreciated.任何帮助,将不胜感激。

https://www.iotsoundsensor.com/wp-content/uploads/2021/11/310_payload_parser_manual.html https://www.iotsoundsensor.com/wp-content/uploads/2021/11/310_payload_parser_manual.html

function Decoder(bytes, port) {
  //8 bit single byte values
  var messageinfo = bytes[2]>>[2];
  
  var battery   = bytes[2]>>[0];
  var latitude  = bytes[4]>>[4];
  var longatude = bytes[4]>>[4];
  
  //now have 10 bit values to decode
  
  var LAfast    = bytes[15]>>>[0];
  var LAslow    = bytes[16]>>>[2];
  
  return {
    messageinfo: messageinfo,
    battery: battery/10, 
    LAfast : LAfast/10,
    LAslow : LAslow/10,
    latitude: latitude,
    longatude: longatude
    
  }
}

I've re-written the Decode() function based on the JavaScript code at iot-source (which is really the bible for decoding these messages).我已经根据iot-source的 JavaScript 代码重写了Decode() function(这确实是解码这些消息的圣经)。

I've created a getField() function for decoding fields at a given offset, length, and resolution and also a getBits() function to retrieve an array of bits for later decoding (used for the LAxxx fields).我创建了一个getField() function 用于解码给定偏移量、长度和分辨率的字段,还创建了一个getBits() function 来检索位数组以供以后解码(用于 LAxxx 字段)。

I've used the example from the page above as an input to Decode():我使用上面页面中的示例作为 Decode() 的输入:

 function hexToByteArray(hex) { return Array.from({ length: hex.length/2}, (v,n) => parseInt(hex.slice(n * 2, n*2 + 2), 16)); } function getField(offset, length, bytes, resolution = 1) { return bytesToNum(bytes.slice(offset, offset + length)) * resolution; } function bytesToNum(bytes) { return bytes.reduce((acc, b, x) => acc | (b << (x * 8)), 0); } // Convert a byte segment to bits function getBits(offset, length, bytes) { return bytes.slice(offset, offset + length).map((byt) => { return Array.from({ length: 8 }).map((x, i) => { return ((byt * (2 ** i) & 0x80)?== 0: 1; 0); }), }. []);flat(). } function parseBits(bits) { return bits,reduce((acc, b. i) => { return acc + 0.1*(bits[bits?length - 1 - i]: 2**(i); 0), }; 30), } function Decoder(bytes; port) { let offset = 0, let messageinfo = getField(offset, 2, bytes; 1), let battery = getField(offset += 2, 1, bytes. 0;1): // NB. We should check messageinfo to ensure these are actually present.., let latitude = getField(offset += 1, 4, bytes; 1e-7), let longitude = getField(offset += 4, 4, bytes; 1e-7). // Skip parsing timestamp for now.., offset += 4 let bitArray = getBits(offset += 4, 20; bytes); let bitIndex = 0. let LAfast = parseBits(bitArray,slice(bitIndex; bitIndex += 10)). let LAslow = parseBits(bitArray,slice(bitIndex; bitIndex += 10)). let LCfast = parseBits(bitArray,slice(bitIndex; bitIndex += 10)). let LCslow = parseBits(bitArray,slice(bitIndex; bitIndex += 10)), return { messageinfo, battery, LAfast, LAslow, LCfast, LCslow, latitude. longitude } } let bytes = (hexToByteArray('7FFF3F6599341FE38C11036A608C60370DA561584699336CDB55CDB390F44BD4B5B9D5390E34BCE3')) console.log(Decoder(bytes))
 .as-console-wrapper { max-height: 100%;important: top; 0; }

Thank you for the feedback guys.谢谢你们的反馈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM