简体   繁体   中英

Python3 string to hex error in file but in terminal not

this is my piece of code which outputs at input clues[0] = '706965':

x = clues[0]
answer = bytes.fromhex(x).decode("ascii")
<class 'ValueError'>, ValueError('non-hexadecimal number found in fromhex() arg at position 5')

If I add 0 to to clues[0] it outputs the expected output with first char with offset.

x = '0'+clues[0]

The output then is '[]ie'. The [] means special char(stackoverflow ommits it).

but if i run this example in terminal everything works fine.

>>> x = '706965'
>>> bytes.fromhex(x).decode("ascii")
'pie'

Solved: Parsing error

From the docs for bytes.fromhex

This bytes class method returns a bytes object, decoding the given string object. The string must contain two hexadecimal digits per byte, with ASCII whitespace being ignored.

Prepending '0' to your string makes it length seven, so it can't consist of two-digit pairs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM