用开泰捕获一个三字节的二进制补码有符号整数

Capture a three bytes two's complement signed integer with Kaitai

Kaitai Struct 提供预定义的类型来捕获,例如,带符号的 2 字节整数 (s2be) 或带符号的 4 字节整数 (s4be),但没有 s3be b24 捕获 3 字节无符号整数 (http://doc.kaitai.io/ksy_reference.html#_bit_size_integers)。有办法吗?

field_a:
    seq:
      - id: two
        type: s2be
      - id: three
        type: ???
      - id: four
        type: s4be

有多种方法可以做到这一点。例如,您可以使用类似这样的方法将无符号转换为有符号:

seq:
  - id: three
    type: s3be
types:
  s3be:
    seq:
      - id: unsigned_value
        type: b24
    instances:
      value:
        value: '(unsigned_value & 0x800000 != 0) ? (~(unsigned_value & 0x7fffff)) : unsigned_value'

请注意,这将是用户类型,因此要获取整数值,您需要使用 three.value,而不仅仅是 three