hbutils.binary.uint

This module provides unsigned integer types for binary I/O operations.

It defines various unsigned integer types with different bit sizes (8, 16, 32, 64 bits) and their corresponding C language aliases. These types support reading from and writing to binary files with proper range validation.

The module uses little-endian byte order for all read/write operations.

CUnsignedIntType

class hbutils.binary.uint.CUnsignedIntType(size: int)[source]

Unsigned integer type for binary I/O operations.

This class provides functionality to read and write unsigned integers of various sizes to/from binary files using little-endian byte order.

__init__(size: int)[source]

Constructor of CUnsignedIntType.

Parameters:

size (int) – Size of the type in bytes.

read(file: BinaryIO) int[source]

Read unsigned int value from binary file.

Reads bytes in little-endian order and converts them to an unsigned integer.

Parameters:

file (BinaryIO) – Binary file object to read from. io.BytesIO is supported as well.

Returns:

Unsigned int value read from the file.

Return type:

int

Example::
>>> import io
>>> from hbutils.binary import c_uint8
>>> with io.BytesIO(b'\xde\xad') as file:
...     print(c_uint8.read(file))
222
property size: int

Size of the given type in bytes.

Returns:

The size of the type.

Return type:

int

write(file: BinaryIO, val: int)[source]

Write unsigned int value to binary IO object.

Writes the integer value as bytes in little-endian order.

Parameters:
  • file (BinaryIO) – Binary file object to write to. io.BytesIO is supported as well.

  • val (int) – Unsigned int value to write.

Raises:
  • TypeError – If val is not an integer.

  • ValueError – If val is outside the valid range for this type.

Example::
>>> import io
>>> from hbutils.binary import c_uint8
>>> with io.BytesIO() as file:
...     c_uint8.write(file, 222)
...     print(file.getvalue())
b'\xde'

c_ubyte

hbutils.binary.uint.c_ubyte = <hbutils.binary.uint.CUnsignedIntType object>

Alias for c_uint8.

Represents an unsigned byte (8-bit unsigned integer).

c_ushort

hbutils.binary.uint.c_ushort = <hbutils.binary.uint.CUnsignedIntType object>

Alias for c_uint16.

Represents an unsigned short integer (16-bit unsigned integer).

c_uint

hbutils.binary.uint.c_uint = <hbutils.binary.uint.CUnsignedIntType object>

Alias for c_uint32 (in 64-bits OS).

Represents an unsigned int. The actual size depends on the platform.

Note

Size of c_uint is the same as that in C language, which mainly depends on CPU and OS.

c_ulong

hbutils.binary.uint.c_ulong = <hbutils.binary.uint.CUnsignedIntType object>

Alias for c_uint64 (in 64-bits OS).

Represents an unsigned long integer. The actual size depends on the platform.

Note

Size of c_ulong is the same as that in C language, which mainly depends on CPU and OS.

c_ulonglong

hbutils.binary.uint.c_ulonglong = <hbutils.binary.uint.CUnsignedIntType object>

Alias for c_uint64 (in 64-bits OS).

Represents an unsigned long long integer (64-bit unsigned integer).

Note

Size of c_ulonglong is the same as that in C language, which mainly depends on CPU and OS.

c_uint8

hbutils.binary.uint.c_uint8 = <hbutils.binary.uint.CUnsignedIntType object>

Reading and writing unsigned integer with 8-bits.

This type represents an unsigned 8-bit integer (range: 0 to 255).

Examples::
>>> import io
>>> from hbutils.binary import c_uint8
>>> 
>>> with io.BytesIO(b'\xde\xad\xbe\xef') as file:
...     print(c_uint8.read(file))
...     print(c_uint8.read(file))
...     print(c_uint8.read(file))
...     print(c_uint8.read(file))
222
173
190
239
>>> with io.BytesIO() as file:
...     c_uint8.write(file, 222)
...     c_uint8.write(file, 173)
...     c_uint8.write(file, 190)
...     c_uint8.write(file, 239)
...     print(file.getvalue())
b'\xde\xad\xbe\xef'

c_uint16

hbutils.binary.uint.c_uint16 = <hbutils.binary.uint.CUnsignedIntType object>

Reading and writing unsigned integer with 16-bits.

This type represents an unsigned 16-bit integer (range: 0 to 65535).

Examples::
>>> import io
>>> from hbutils.binary import c_uint16
>>> 
>>> with io.BytesIO(b'\xde\xad\xbe\xef\x12\x34\x56\x78') as file:
...     print(c_uint16.read(file))
...     print(c_uint16.read(file))
...     print(c_uint16.read(file))
...     print(c_uint16.read(file))
44510
61374
13330
30806
>>> with io.BytesIO() as file:
...     c_uint16.write(file, 44510)
...     c_uint16.write(file, 61374)
...     c_uint16.write(file, 13330)
...     c_uint16.write(file, 30806)
...     print(file.getvalue())
b'\xde\xad\xbe\xef\x124Vx'

c_uint32

hbutils.binary.uint.c_uint32 = <hbutils.binary.uint.CUnsignedIntType object>

Reading and writing unsigned integer with 32-bits.

This type represents an unsigned 32-bit integer (range: 0 to 4294967295).

Examples::
>>> import io
>>> from hbutils.binary import c_uint32
>>> 
>>> with io.BytesIO(b'\xde\xad\xbe\xef\x12\x34\x56\x78') as file:
...     print(c_uint32.read(file))
...     print(c_uint32.read(file))
4022250974
2018915346
>>> with io.BytesIO() as file:
...     c_uint32.write(file, 4022250974)
...     c_uint32.write(file, 2018915346)
...     print(file.getvalue())
b'\xde\xad\xbe\xef\x124Vx'

c_uint64

hbutils.binary.uint.c_uint64 = <hbutils.binary.uint.CUnsignedIntType object>

Reading and writing unsigned integer with 64-bits.

This type represents an unsigned 64-bit integer (range: 0 to 18446744073709551615).

Examples::
>>> import io
>>> from hbutils.binary import c_uint64
>>> 
>>> with io.BytesIO(b'\xde\xad\xbe\xef\x12\x34\x56\x78') as file:
...     print(c_uint64.read(file))
8671175388484775390
>>> with io.BytesIO() as file:
...     c_uint64.write(file, 0x78563412efbeadde)
...     print(file.getvalue())
b'\xde\xad\xbe\xef\x124Vx'