bytes

Module with some bytes utility functions.

class BytesUtils

Bases: object

Class container for bytes utility functions.

static Reverse(data_bytes: bytes) bytes

Reverse the specified bytes.

Parameters:

data_bytes (bytes) – Data bytes

Returns:

Original bytes in the reverse order

Return type:

bytes

static Xor(data_bytes_1: bytes, data_bytes_2: bytes) bytes

XOR the specified bytes.

Parameters:
  • data_bytes_1 (bytes) – Data bytes 1

  • data_bytes_2 (bytes) – Data bytes 2

Returns:

XORed bytes

Return type:

bytes

static AddNoCarry(data_bytes_1: bytes, data_bytes_2: bytes) bytes

Add the specified bytes (byte-by-byte, no carry).

Parameters:
  • data_bytes_1 (bytes) – Data bytes 1

  • data_bytes_2 (bytes) – Data bytes 2

Returns:

XORed bytes

Return type:

bytes

static MultiplyScalarNoCarry(data_bytes: bytes, scalar: int) bytes

Multiply the specified bytes with the specified scalar (byte-by-byte, no carry).

Parameters:
  • data_bytes (bytes) – Data bytes

  • scalar (int) – Scalar

Returns:

XORed bytes

Return type:

bytes

static ToBinaryStr(data_bytes: bytes, zero_pad_bit_len: int = 0) str

Convert the specified bytes to a binary string.

Parameters:
  • data_bytes (bytes) – Data bytes

  • zero_pad_bit_len (int, optional) – Zero pad length in bits, 0 if not specified

Returns:

Binary string

Return type:

str

static ToInteger(data_bytes: bytes, endianness: typing_extensions.Literal[little, big] = 'big', signed: bool = False) int

Convert the specified bytes to integer.

Parameters:
  • data_bytes (bytes) – Data bytes

  • endianness ("big" or "little", optional) – Endianness (default: big)

  • signed (bool, optional) – True if signed, false otherwise (default: false)

Returns:

Integer representation

Return type:

int

static FromBinaryStr(data: Union[bytes, str], zero_pad_byte_len: int = 0) bytes

Convert the specified binary string to bytes.

Parameters:
  • data (str or bytes) – Data

  • zero_pad_byte_len (int, optional) – Zero pad length in bytes, 0 if not specified

Returns:

Bytes representation

Return type:

bytes

static ToHexString(data_bytes: bytes, encoding: str = 'utf-8') str

Convert bytes to hex string.

Parameters:
  • data_bytes (bytes) – Data bytes

  • encoding (str, optional) – Encoding type, utf-8 by default

Returns:

Bytes converted to hex string

Return type:

str

static FromHexString(data: Union[bytes, str]) bytes

Convert hex string to bytes.

Parameters:

data (str or bytes) – Data bytes

Returns

bytes: Hex string converted to bytes

static FromList(data_list: List[int]) bytes

Convert the specified list of integers to bytes.

Parameters:

data_list (list[int]) – List of integers

Returns:

Bytes representation

Return type:

bytes

static ToList(data_bytes: bytes) List[int]

Convert the specified bytes to a list of integers.

Parameters:

data_bytes (bytes) – Data bytes

Returns:

List of integers

Return type:

list[int]