Skip to Content
Binal
Getting Started

Quick Start

Define your first binary model

from typing import Annotated

from binal import BinaryModel, Prefixed, UInt8, UInt16


class Packet(BinaryModel, byte_order="big"):
    version: UInt8
    payload: Annotated[bytes, Prefixed(UInt16)]


packet = Packet(version=1, payload=b"Hello")
data = packet.encode()
print(data)  # b'\x01\x00\x05Hello'

packet = Packet.decode(data)
print(packet)  # Packet(version=1, payload=b'Hello')

Encoding and decoding use the same schema, so the binary layout is defined exactly once, in the model.