Ad

Unpack Dec From Hex - Via Bit Offsets

- 1 answer

I have a block of hex data which inicludes settings of a sensor, I will include the beginning snippet of the hex (LSB first):


F501517C 8150D4DE 04010200 70010101 05F32A04 F4467000 00000AFF 0502D402


This comes straight from the documentation to decode this hex to dec:

3.1. Full identifier and settings record (0x7C)

Offset   Length (bytes)    Field description

0x00     6                 Full identifier
0x06     40                Settings

3.1.1 Full identifier

Offset      Field description

0x00        Product Type
0x01        Device Type
0x02        Software Major Version
0x03        Software Minor Version
0x04        Hardware Major Version
0x05        Hardware Minor Version

3.1.2 Settings

Offset   Length(bit)   Offset(bit)  Default value   Min   Max     Field Description
0x00     8             0            0               0     255     Country number
0x01     8             0            0               0     255     District number
0x02     16            0            0               0     9999    Sensor number
...
0x27

This being the only information I have to decode this. The offset column must be the trick to understanding this.

  1. What are the hex values offset from?
  2. I see 7C in the first hex string.
  3. The Settings section goes to 0x27 = 39 in decimal which is stated in the 3.1 section as the length being 40.
Ad

Answer

The given hex bytes are byte offset from the beginning of the data.

Assuming that your given dump is little endian 32-bit, let's have a look:

Value in dump - separated in bytes - bytes in memory
F501517C      - F5 01 51 7C        - 7C 51 01 F5
8150D4DE      - 81 50 D4 DE        - DE D4 50 81
04010200      - 04 01 02 00        - 00 02 01 04

Now let's assign them to the fields. The next list has both records concatenated.

Byte  Offset      Field description

7C    0x00        Product Type
51    0x01        Device Type
01    0x02        Software Major Version
F5    0x03        Software Minor Version
DE    0x04        Hardware Major Version
D4    0x05        Hardware Minor Version

Byte  Offset   Length(bit)   Offset(bit)  Default value   Min   Max     Field Description
50    0x00     8             0            0               0     255     Country number
81    0x01     8             0            0               0     255     District number
00,02 0x02     16            0            0               0     9999    Sensor number

Whether the result makes sense, is your decision:

  • Product Type = 0x7C
  • Device Type = 0x51 = 81 decimal (could also be ASCII 'Q')
  • Software Major.Minor Version = 0x01.0xF5 = 1.245 decimal
  • Hardware Major.Minor Version = 0xDE.0xD4 = 222.212
  • Country number = 0x50 = 80 decimal (could also be ASCII 'P')
  • District number = 0x81 = 129 decimal (perhaps 0x01 = 1 with bit 7 set?)
  • Sensor number = 0x0002 = 2 decimal (big endian assumed)
Ad
source: stackoverflow.com
Ad