Binary Packet Encoder/Decoder

Medium Python Protocols
Solve this problem

Implement Python functions to encode and decode binary packets with STX, payload, checksum, and ETX. Checksum = sum(payload) % 256.

Constraints

  • STX=0x02, ETX=0x03.
  • encode(payload) returns list of bytes.
  • decode(packet) validates checksum and returns payload.
  • Return None on failure.
  • Payload is list of ints.
Starter codePython
1def encode(payload):2    pass3 4def decode(pkt):5    pass