Integer Fields

Integer fields parse and serialize fixed-size integer values.

Available Types

Field

Size

Range

Int8

1 byte

-128 to 127

UInt8

1 byte

0 to 255

Int16

2 bytes

-32,768 to 32,767

UInt16

2 bytes

0 to 65,535

Int32

4 bytes

-2^31 to 2^31-1

UInt32

4 bytes

0 to 2^32-1

Int64

8 bytes

-2^63 to 2^63-1

UInt64

8 bytes

0 to 2^64-1

Basic Usage

from pystructs import Struct, UInt8, UInt16, Int32

class Header(Struct):
    version = UInt8(default=1)
    flags = UInt16()
    offset = Int32()

header = Header.parse(b"\x01\x0f\x00\xff\xff\xff\xff")
print(header.version)  # 1
print(header.flags)    # 15
print(header.offset)   # -1

Endianness

By default, integers use little-endian byte order. Override with Meta.endian or per-field endian parameter:

class NetworkHeader(Struct):
    class Meta:
        endian = "big"

    port = UInt16()
    sequence = UInt32()

# Per-field override
class MixedEndian(Struct):
    le_value = UInt32()  # Little-endian (default)
    be_value = UInt32(endian="big")  # Big-endian

API Reference

class pystructs.fields.integers.IntField(default: int = 0, endian: str | None = None, required: bool = True, validators: List[Callable] | None = None)[source]

Base class for integer fields.

Supports configurable byte order (endianness) at field or struct level.

parse(buffer: BinaryIO, instance: Struct) int[source]

Parse an integer from the buffer.

Args:

buffer: Binary stream to read from instance: The struct instance being parsed

Returns:

Parsed integer value

Raises:

UnexpectedEOF: If not enough bytes available

serialize(value: int, instance: Struct) bytes[source]

Serialize an integer to bytes.

Args:

value: Integer value to serialize instance: The struct instance being serialized

Returns:

Serialized bytes

class pystructs.fields.integers.Int8(default: int = 0, endian: str | None = None, required: bool = True, validators: List[Callable] | None = None)[source]

8-bit signed integer (-128 to 127).

class pystructs.fields.integers.UInt8(default: int = 0, endian: str | None = None, required: bool = True, validators: List[Callable] | None = None)[source]

8-bit unsigned integer (0 to 255).

class pystructs.fields.integers.Int16(default: int = 0, endian: str | None = None, required: bool = True, validators: List[Callable] | None = None)[source]

16-bit signed integer (-32768 to 32767).

class pystructs.fields.integers.UInt16(default: int = 0, endian: str | None = None, required: bool = True, validators: List[Callable] | None = None)[source]

16-bit unsigned integer (0 to 65535).

class pystructs.fields.integers.Int32(default: int = 0, endian: str | None = None, required: bool = True, validators: List[Callable] | None = None)[source]

32-bit signed integer.

class pystructs.fields.integers.UInt32(default: int = 0, endian: str | None = None, required: bool = True, validators: List[Callable] | None = None)[source]

32-bit unsigned integer.

class pystructs.fields.integers.Int64(default: int = 0, endian: str | None = None, required: bool = True, validators: List[Callable] | None = None)[source]

64-bit signed integer.

class pystructs.fields.integers.UInt64(default: int = 0, endian: str | None = None, required: bool = True, validators: List[Callable] | None = None)[source]

64-bit unsigned integer.