Nadeem Mehraj
Nadeem Mehraj

Reputation: 184

ValueError: non-hexadecimal number found in fromhex() arg at position

I have a hexadecimal string

hexDecoded = '0xa506f7374696e6720446174653a204a756c792031322c2032303038205b45426f6f6b20233238395d0a52656c6561736520446174653a204a756c792c20313939350a5b4c61737420757064617465643a204a616e756172792031332c20323031325d0a0a0a4c616e67756167653a20456e676c6973680a0a0a2a2a2a205354'

On trying to obtain text as

text = bytearray.fromhex(hexDecoded.lstrip('0x')).decode()

I get the error

ValueError: non-hexadecimal number found in fromhex() arg at position 255

I can't understand why the code is looking at postion 255 in the hexadecimal string when the length of stripped hexadecimal string is only 255.

Any ideas what is missing here.

Thanks for help

Upvotes: 6

Views: 12412

Answers (1)

John Gordon
John Gordon

Reputation: 33275

It's because that string contains an odd number of digits, leaving the trailing 4 as a singleton.

bytearray.fromhex('4') produces the same error.

If you change it to 04, it works.

Upvotes: 12

Related Questions