def bytes(self, include_crc = True): """bytes(include_crc = True) -> bytearray Returns a representation of the header in packed binary format. If include_crc is False, the CRC will be omitted regardless of the header.crc flag. """ buf = bytearray(self._FORMAT.length) bitpack_into(self._FORMAT, buf, 0, *[self.__dict__[k] for k in self._FIELDS]) pos = len(buf) buf.extend(self._side_info) if include_crc and self.crc: crc = crc16(memoryview(buf)[2:]) buf[pos:pos] = 2 # Inserts two bytes at pos struct.pack_into('>H', buf, pos, crc) return buf
def bitpack(self, fmt, offset = 0, *vals): fmt = isinstance(fmt, _bitpack.formatstr) and fmt or _bitpack.formatstr(fmt) self._bitpack_check_length(fmt, offset) return _bitpack.bitpack_into(fmt, self._buffer, self._pos + offset, *vals)