I'm trying to convert an hex value to string but i keep getting this error
Traceback (most recent call last): File
"C:\Users\ASUS\Desktop\parse.py", line 14, in
data = bytes.fromhex(''.join(map(str,tup[4:4+length]))).decode("utf8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x91 in position 0: invalid start byte
I don't know python well, it's like my first time using python 3. Here's the code:
import binascii
import struct
hex ="01 06 1C 02 5B 90 10 6F 01 03 3C 04 01 01 03 07 01 00 01 03 07 1E 01 01 09 05 15 00 00 04 54 52 2D 31 01 11 05 02 00 00 00 00 00 01 01 00 00 00 00 00 00 00 00 01 27 01 01 00 23 08 09 01 2A 41 73 64 23 31 23 31 23 30 23 31 30 30 30 23 30 23 30 2C 30 2C 30 23 30 23 23 30 23 23 23 30 01 04 05 16 00 28 01 03 05 0A 00 01 09 01 01 00 05 08 15 01 31 01 01 06 01 01 00 02 05 07 "
p1 = binascii.unhexlify(''.join(hex.split()))
print(p1)
print("\n\n")
tup = struct.unpack(str(len(p1))+'B', p1)
if tup[0] == 1:
# <= 0xFF
length = tup[1] - 2
C = tup[2]
CC = tup[3]
print("C: "+str(C)+", CC: "+str(CC)+" Size: "+str(length))
nHex = ''.join(map(str,tup[4:4+length]))
# code below is from another stackoverflow answer and can be replaced if you got a better one
data = bytes.fromhex(nHex).decode("utf8")
print("".join(chr(c) if chr(c) in string.printable else '.' for c in data))
I should get a timestamp as a string after decoding the hex. How can I convert nHex
to string?