hbutils.binary.bool

Boolean type handling for binary I/O operations using C-style conventions.

This module provides utilities for reading and writing boolean values in binary streams following the C language representation of bool. The core functionality is implemented by CBoolType, which reads and writes fixed-size boolean values. A pre-configured c_bool instance is provided to match the platform’s native C bool size, ensuring compatibility with C binary data structures.

The module contains the following public components:

  • CBoolType - Fixed-size boolean type for binary I/O.

  • c_bool - Pre-configured boolean type instance based on C bool size.

Example:

>>> import io
>>> from hbutils.binary import c_bool
>>> with io.BytesIO() as file:
...     c_bool.write(file, True)
...     c_bool.write(file, False)
...     file.getvalue()
b'\x01\x00'

Note

The boolean value is encoded with 0x01 for True and 0x00 for False, padded with leading zeros to match the configured size.

__all__

hbutils.binary.bool.__all__ = ['CBoolType', 'c_bool']

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_bool

hbutils.binary.bool.c_bool = <hbutils.binary.bool.CBoolType object>

Pre-configured boolean type instance for reading and writing bool values in C language format.

This instance is configured with the size of C’s bool type on the current platform, ensuring compatibility with C binary data structures.

Type:

CBoolType

Examples:

>>> import io
>>> from hbutils.binary import c_bool
>>> 
>>> # Reading boolean values
>>> with io.BytesIO(b'\x01\x00\x01\x00') as file:
...     print(c_bool.read(file))
...     print(c_bool.read(file))
...     print(c_bool.read(file))
...     print(c_bool.read(file))
True
False
True
False

>>> # Writing boolean values
>>> with io.BytesIO() as file:
...     c_bool.write(file, True)
...     c_bool.write(file, False)
...     c_bool.write(file, True)
...     c_bool.write(file, False)
...     print(file.getvalue())
... 
b'\x01\x00\x01\x00'

CBoolType

class hbutils.binary.bool.CBoolType(size: int)[source]

Boolean type for binary I/O operations.

This class provides methods to read and write boolean values in binary format, compatible with C language boolean representation.

Parameters:

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

Example:

>>> import io
>>> from hbutils.binary import CBoolType
>>> c_bool_1 = CBoolType(1)
>>> with io.BytesIO(b'\x01\x00') as file:
...     c_bool_1.read(file), c_bool_1.read(file)
(True, False)
__init__(size: int)[source]

Constructor of CBoolType.

Parameters:

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

read(file: BinaryIO) bool[source]

Read boolean value from binary file.

The method reads exactly size bytes and returns True if any of the bytes is non-zero; otherwise, it returns False.

Parameters:

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

Returns:

Boolean value read from the file.

Return type:

bool

Example:

>>> import io
>>> from hbutils.binary import c_bool
>>> with io.BytesIO(b'\x01\x00') as file:
...     print(c_bool.read(file))
...     print(c_bool.read(file))
True
False
write(file: BinaryIO, val: bool) None[source]

Write boolean value to binary IO object.

The boolean value is written as a sequence of bytes with the size specified during initialization. The value is represented as 0x01 for True and 0x00 for False, padded with leading zeros to match the required size.

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

  • val (bool) – Boolean value to write.

Returns:

None.

Return type:

None

Example:

>>> import io
>>> from hbutils.binary import c_bool
>>> with io.BytesIO() as file:
...     c_bool.write(file, True)
...     c_bool.write(file, False)
...     print(file.getvalue())
b'\x01\x00'