-3

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?

4
  • What is the expected output exactly? Commented Sep 5, 2018 at 18:05
  • @OlivierMelançon a timestamp created using System.currentTimeMillis() / 1000 in Java Commented Sep 5, 2018 at 18:06
  • 1
    You have a print statement. What exactly should it output from the given input. Show us the exact string. Commented Sep 5, 2018 at 18:07
  • @OlivierMelançon i really don't know the exact string but i'm pretty sure nHex contains a timestamp just like 1536170968 Commented Sep 5, 2018 at 18:10

1 Answer 1

1

You are getting this error because 0x91, or 145 in decimal, is not a valid ascii value.

You can use int(_, 16) to convert an hexadecimal string to its integer value and then chr to convert this integer to the corresponding unicode character.

hex_ = "74 69 6d 65 73 74 61 6D 70"

s = ''.join([chr(int(x, 16)) for x in hex_.split()])

print(s)

Output

timestamp

As a sidenote, avoid using hex as a variable since it overwrites the builtin function hex.

Sign up to request clarification or add additional context in comments.

5 Comments

It prints ńđ, is that what nHex contains or an unicode error? I was pretty sure it's a timestamp.
A more readable equivalent of your code: bytearray.fromhex(hex).decode(). Works for both python 2 and 3
@party34 As I said, if you don't show the expected output, I cannot help you. How did you obtain your string exactly?
@OlivierMelançon I obtained it using WPE PRO while trying to see what exactly my server sends to the client.
@LakshayGarg That also throws the same error in the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.