I have an instrument that returns a string with the format:
StrInstrument = '\xE0\x31\xFF\xCF\xFF\xCA\xFF\xC4'
I have to perform operations with the data in the string. In order to do so, in the code I should convert it from little endian with the function struct.unpack(). I tried to interpret the string as bytes using the bytes() function but it does not work, probably because it tries to convert my string to bytes instead of changing the interpretation:
print(bytes(StrInstrument,'utf-8'))
iValues = struct.unpack('HHHH',bytes(StrInstrument,'utf-8'))
print(iValues)
b'\xc3\xa01\xc3\xbf\xc3\x8f\xc3\xbf\xc3\x8a\xc3\xbf\xc3\x84' error: unpack requires a buffer of 8 bytes
After changing the buffer to 'HHHHHHHH':
b'\xc3\xa01\xc3\xbf\xc3\x8f\xc3\xbf\xc3\x8a\xc3\xbf\xc3\x84' error: unpack requires a buffer of 16 bytes
Instead, the code works fine if I simply write this:
iValues = struct.unpack('HHHH',b'\xE0\x31\xFF\xCF\xFF\xCA\xFF\xC4')
print(iValues)
(12768, 53247, 51967, 50431)
Is there a way to do this without writing the string in the function?