[docs]defdata_to_string(data:Sequence[int],msb_first:bool=True,endianness:str="=",)->bytes:r"""Converts a list of unsigned 16-bit integers to a string of bytes. Parameters ---------- data A list of 16-bit unsigned integers. msb_first If `True`, the sequence is understood as ordered from most significant byte to least significant byte. If `False`, LSB to MSB. endianness The endianness of the data. Defaults to the system endianness (``=``). Valid values are ``=`` (system endianness), ``<`` (little-endian), and ``>`` (big-endian). Returns ------- bytes A string of bytes. Example ------- .. code-block:: python >>> data_to_string([10, 20], msb_first=True, endianness="<") b'\x14\x00\n\x00' >>> data_to_string([10, 20], msb_first=False, endianness="<") b'\n\x00\x14\x00' """ifmsb_first:data_ordered=data[::-1]else:data_ordered=datareturnstruct.pack(f"{endianness}"+("H"*len(data)),*data_ordered)
[docs]defdata_to_uint16(data:int,endianness:str="=")->int:"""Converts an unsigned 16-bit integer to an integer. This essentially handles the possibility that the data is not in the system endianness. Parameters ---------- data The 16-bit unsigned integer. endianness The endianness of the data. Defaults to the system endianness (``=``). Valid values are ``=`` (system endianness), ``<`` (little-endian), and ``>`` (big-endian). Returns ------- int The 16-bit unsigned integer. """returnstruct.unpack("=H",data_to_string([data],endianness=endianness))[0]
[docs]defdata_to_uint32(data:tuple[int,int],**kwargs)->int:"""Converts a tuple of two 16-bit unsigned integers to a 32-bit unsigned integer. Parameters ---------- data A tuple of two 16-bit unsigned integers. kwargs Additional arguments to pass to `.data_to_string`. Returns ------- int The 32-bit unsigned integer. """returnstruct.unpack("=I",data_to_string(data,**kwargs))[0]
[docs]defdata_to_int32(data:tuple[int,int],**kwargs)->int:"""Converts a tuple of two 16-bit unsigned integers to a 32-bit signed integer. Parameters ---------- data A tuple of two 16-bit unsigned integers. kwargs Additional arguments to pass to `.data_to_string`. Returns ------- int The 32-bit signed integer. """returnstruct.unpack("=i",data_to_string(data,**kwargs))[0]
[docs]defdata_to_float32(data:tuple[int,int],**kwargs)->float:"""Converts a tuple of two 16-bit unsigned integers to a 32-bit float. Parameters ---------- data A tuple of two 16-bit unsigned integers. kwargs Additional arguments to pass to `.data_to_string`. Returns ------- float The 32-bit float. """returnstruct.unpack("=f",data_to_string(data,**kwargs))[0]