########################################################################### # # Copyright (c) 2020-2024 Diality Inc. - All Rights Reserved. # # THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN # WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. # # @file conversions.py # # @author (last) Dara Navaei # @date (last) 20-Jun-2022 # @author (original) Peter Lucia # @date (original) 02-Apr-2020 # ############################################################################ import struct from typing import List, Union # https://docs.python.org/3/library/struct.html#format-characters def byte_to_bytearray(val: int) -> bytes: """ Converts a byte value into a byte array (little endian) @param val: (int) integer to convert to byte array @return: byte array """ if type(val) != int: raise ValueError("Expected integer but received {0} with type {1}".format(val, type(val))) return struct.pack(" bytes: """ Converts a byte value into a byte array (little endian) @param val: (int) integer to convert to byte array @return: byte array """ if type(val) != int: raise ValueError("Expected integer but received {0} with type {1}".format(val, type(val))) return struct.pack(" bytes: """ Converts a short integer (2 bytes) value into a byte array (little endian) @param val: (int) integer to convert to byte array @return: byte array """ if type(val) != int: raise ValueError("Expected integer but received {0} with type {1}".format(val, type(val))) return struct.pack(" bytes: """ Converts an unsigned short integer (2 bytes) value into a byte array (little endian) @param val: (int) integer to convert to byte array @return: byte array """ if type(val) != int or val < 0: raise ValueError("Expected unsigned integer but received {0} with type {1}".format(val, type(val))) return struct.pack(" bytes: """ Converts an integer value into a byte array (little endian) @param val: integer to convert to byte array, bool is accepted @return: byte array """ if type(val) != bool and type(val) != int: raise ValueError("Expected integer but received {0} with type {1}".format(val, type(val))) return struct.pack(" bytes: """ Converts an integer value into a byte array (little endian) @param val: (int) integer to convert to byte array @return: byte array """ if type(val) != int or val < 0: raise ValueError("Expected unsigned integer but received {0} with type {1}".format(val, type(val))) return struct.pack(" bytes: """ Converts a float value into a byte array (little endian) @param val: float to convert to byte array, int is accepted @return:: byte array """ if type(val) != float and type(val) != int: raise ValueError("Expected float but received {0} with type {1}".format(val, type(val))) return struct.pack(' List[int]: """ Converts an integer to a bit array. For direct conversion to binary please use bin(val), as this function has a separate purpose @param val: (int) the number to convert @param num_bits: (int) the number of bits needed @return: (List[int]) The number represented in binary form as a list of 1's and 0's """ if type(val) != int: raise ValueError("Expected integer but received {0} with type {1}".format(val, type(val))) return [(val >> bit) & 1 for bit in range(num_bits - 1, -1, -1)] def bytearray_to_value(fmt: str, buf: bytes) -> int: """ Converts the buffer to the type of value specified in the frm as format @param fmt: string format to convert the buffer to. @param buf: the buffer of bytes to be converted to the value @return: the converted value of buffer to the type of fmt if successful or None if not. """ value = None try: value = struct.unpack(fmt, bytearray(buf)) except BaseException as err: print("err: ", err) if value is not None and len(value) > 0: value = value[0] return value def bytearray_to_byte(buffer: bytes, index: int, signed: bool = True) -> (int, int): """ Converts the buffer of bytes to a value of one(1) byte integer @param buffer: (bytes) the source buffer @param index: (int) the start index of reading the source buffer @param signed: (bool) convert to signed or unsigned value @return: The pair of the value and incremented index by the length. """ length = 1 # for a byte value = bytearray_to_value("b" if signed else "B", buffer[index:index + length]) return value, index + length def bytearray_to_short(buffer: bytes, index: int, signed: bool = True) -> [int, int]: """ Converts the buffer of bytes to a value of two(2) byte integer @param buffer: (bytes) the source buffer @param index: (int) the start index of reading the source buffer @param signed: (bool) convert to signed or unsigned value @return: pair of the [value and incremented index by the length. """ length = 2 # for a short value = bytearray_to_value("h" if signed else "H", buffer[index:index + length]) return value, index + length def bytearray_to_integer(buffer: bytes, index: int, signed: bool = True) -> (int, int): """ Converts the buffer of bytes to a value of four(4) byte integer @param buffer: (bytes) the source buffer @param index: (int) the start index of reading the source buffer @param signed: (bool) convert to signed or unsigned value @return: The pair of the value and incremented index by the length. """ length = 4 # for an integer value = bytearray_to_value("i" if signed else "I", buffer[index:index + length]) return value, index + length def bytearray_to_long(buffer: bytes, index: int, signed: bool = True) -> [int, int]: """ Converts the buffer of bytes to a value of eight(8) byte integer @param buffer: (bytes) the source buffer @param index: (int) the start index of reading the source buffer @param signed: (bool) convert to signed or unsigned value @return: pair of the [value and incremented index by the length. """ length = 8 # for a long value = bytearray_to_value("q" if signed else "Q", buffer[index:index + length]) return value, index + length def bytearray_to_float(buffer: bytes, index: int, is_double: bool = False) -> [float, int]: """ Converts the buffer of bytes to a value of floating point. @param buffer: (bytes) the source buffer @param index: (int) the start index of reading the source buffer @param is_double: (bool) convert the value to eight(8) byte floating point if is_double is True, or four(4) bytes otherwise. @return: pair of the [value and incremented index by the length. """ length = 8 if is_double else 4 value = bytearray_to_value("d" if is_double else "f", buffer[index:index + length]) return value, index + length