Byte Utils

class jam.utils.byte_utils.ByteUtils[source]

Bases: object

Utility class for converting between different byte-related data types

static int_to_bytes(value: int, length: int = None, byteorder: Literal['big', 'little'] = 'big') bytes[source]

Convert integer to bytes with optional fixed length

static bytes_to_int(value: bytes, byteorder: Literal['big', 'little'] = 'big') int[source]

Convert bytes to integer

static hex_to_bytes(hex_str: str) bytes[source]

Convert hex string to bytes

static bytes_to_hex(value: bytes) str[source]

Convert bytes to hex string

static to_bytes(value: int | bool | bytes | str | bytearray | memoryview | Sequence) bytearray[source]

Convert [str (hex_string), int, bool, bytes, memoryview] to bytearray

static ensure_valid_sequence(value: Sequence) bool[source]

Check if a sequence is valid

Checks if all elements in the sequence are same type

Parameters:

value – Sequence to check

Returns:

True if the sequence is valid, False otherwise

static bool_to_bytes(value: bool) bytes[source]

Convert boolean to bytes

static bytes_to_bool(value: bytes) bool[source]

Convert bytes to boolean

static bitarray_to_bytes(bits: List[bool], bitorder: Literal['msb', 'lsb'] = 'msb') bytes[source]

Convert bit array to bytes

Parameters:
  • bits – List of boolean values representing bits

  • bitorder – If “msb” (default), treats first bit as most significant. If “lsb”, treats first bit as least significant.

static bytes_to_bitarray(value: bytes, bitorder: Literal['msb', 'lsb'] = 'msb', target_length: int | None = None) List[bool][source]

Convert bytes to bit array

Parameters:
  • value – Bytes to convert

  • bitorder – If “msb” (default), outputs bits with most significant first. If “lsb”, outputs bits with the least significant first.

static str_to_bytes(value: str, encoding: str = 'utf-8') bytes[source]

Convert string to bytes

static bytes_to_str(value: bytes, encoding: str = 'utf-8') str[source]

Convert bytes to string

classmethod hex_to_bitarray(hex_str: str) List[bool][source]

Convert hex string to bit array

classmethod bitarray_to_hex(bits: List[bool]) str[source]

Convert bit array to hex string

classmethod bitarray_to_int(bits: List[bool], bitorder: Literal['msb', 'lsb'] = 'msb') int[source]

Convert bit array to integer

classmethod hex_to_int(hex_str: str) int[source]

Convert hex string to integer

classmethod int_to_hex(value: int, length: int | None = None) str[source]

Convert integer to hex string

Parameters:
  • value – Integer to convert

  • length – Optional number of bytes to pad to

Returns:

Hex string without ‘0x’ prefix

static bytable_to_bitarray(value: bytearray) List[bool][source]

Convert bytearray to bitarray

static int_to_bitarray(n: int) list[bool][source]

Convert an integer to its binary representation as a list of booleans. The least significant bit is at index 0.

Parameters:

n – The integer to convert

Returns:

A list of booleans where True represents 1 and False represents 0

Examples

>>> int_to_bits(5)  # 5 is 101 in binary
[True, False, True]
>>> int_to_bits(0)
[False]