hbutils.binary.uint
Unsigned integer types for binary I/O operations.
This module defines a set of unsigned integer types with fixed sizes
(8, 16, 32, and 64 bits) and provides facilities to read and write these
values to binary streams using little-endian byte order. It also exposes
aliases that mirror common C unsigned integer types through ctypes.
The module contains the following main components:
CUnsignedIntType- Unsigned integer type with fixed size and rangec_uint8,c_uint16,c_uint32,c_uint64- Fixed-size unsigned typesc_ubyte,c_ushort,c_uint,c_ulong,c_ulonglong- C aliases
Note
All read/write operations are little-endian. Ensure the file-like object is opened in binary mode.
Example:
>>> import io
>>> from hbutils.binary import c_uint16
>>> with io.BytesIO() as file:
... c_uint16.write(file, 65535)
... file.seek(0)
... print(c_uint16.read(file))
65535
__all__
- hbutils.binary.uint.__all__ = ['CUnsignedIntType', 'c_uint8', 'c_uint16', 'c_uint32', 'c_uint64', 'c_ubyte', 'c_ushort', 'c_uint', 'c_ulong', 'c_ulonglong']
Built-in mutable sequence.
If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.
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'
c_ubyte
c_ushort
c_uint
c_ulong
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_ulonglongis the same as that in C language, which mainly depends on CPU and OS.
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.
- Parameters:
size (int) – Size of the type in bytes.
- Variables:
size (int) – Size of the type in bytes, inherited from
CRangedIntType.minimum (int) – Minimum representable value (always 0).
maximum (int) – Maximum representable value for the given size.
- __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.BytesIOis 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
- write(file: BinaryIO, val: int) None[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.BytesIOis supported as well.val (int) – Unsigned int value to write.
- Returns:
None.- Return type:
None
- 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'