Byte Utils
- class jam.utils.byte_utils.ByteUtils[source]
Bases:
objectUtility 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 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 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.
- classmethod bitarray_to_int(bits: List[bool], bitorder: Literal['msb', 'lsb'] = 'msb') int[source]
Convert bit array 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 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]