Convert from ASCII string encoded in Hex to plain ASCII?
561,540
Solution 1
A slightly simpler solution:
>>> "7061756c".decode("hex")
'paul'
Solution 2
No need to import any library:
>>> bytearray.fromhex("7061756c").decode()
'paul'
Solution 3
>>> txt = '7061756c'
>>> ''.join([chr(int(''.join(c), 16)) for c in zip(txt[0::2],txt[1::2])])
'paul'
i'm just having fun, but the important parts are:
>>> int('0a',16) # parse hex
10
>>> ''.join(['a', 'b']) # join characters
'ab'
>>> 'abcd'[0::2] # alternates
'ac'
>>> zip('abc', '123') # pair up
[('a', '1'), ('b', '2'), ('c', '3')]
>>> chr(32) # ascii to character
' '
will look at binascii now...
>>> print binascii.unhexlify('7061756c')
paul
cool (and i have no idea why other people want to make you jump through hoops before they'll help).
Solution 4
In Python 2:
>>> "7061756c".decode("hex")
'paul'
In Python 3:
>>> bytes.fromhex('7061756c').decode('utf-8')
'paul'
Solution 5
b''.fromhex('7061756c')
use it without delimiter
Related videos on Youtube
Author by
JanusFox81
I am interested in functional programming. I am also interested in music programming, such as in ChucK or impromptu. My favorite language is Lisp.
Updated on January 18, 2022Comments
-
JanusFox81 over 2 years
How can I convert from hex to plain ASCII in Python?
Note that, for example, I want to convert "0x7061756c" to "paul".
-
JanusFox81 about 12 yearsI've tried a bunch of stuff I found here: docs.python.org/library/binascii.html
-
Vincent Savard about 12 yearsWith the help of the link you just gave us, I found the function you were looking for. What exactly did you try and why didn't it work?
-
JanusFox81 about 12 yearsI tried the following: >>> binascii.b2a_hqx("0x7061756c") '-(Jh-$Ba0c8fB`' >>> binascii.b2a_uu("0x7061756c") "*,'@W,#8Q-S4V8P \n" >>> binascii.b2a_base64("0x7061756c") 'MHg3MDYxNzU2Yw==\n' >>> binascii.b2a_qp("0x7061756c") '0x7061756c' >>> binascii.b2a_hex("0x7061756c") '30783730363137353663' >>> binascii.b2a_hex(0x7061756c) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: must be string or buffer, not int >>>
-
JanusFox81 about 12 yearsNone of them worked, because none of them returned 'paul'.
-
Admin about 12 yearsDon't you mean "7-bit" ASCII? (Which is sort of silly because ASCII is only 7-bits.) A GUID is 128bits...
-
-
jfs about 12 yearsthere is no
.decode('hex')
on Python 3..decode('hex')
usesbinascii.unhexlify()
on Python 2. -
cjm about 12 yearsThanks for pointing that out, I'm not as familiar with Python 3. This solution also won't work in 1 as far as I know.
-
Mark Lakata over 10 years+1 for a useful example, but you are not converting "hex" as the input but you are converting any integer to a hex string. You code will work equally as well with
print convert_hex_to_ascii(123456)
. -
Mark Evans almost 9 years
codecs.decode("7061756c", "hex")
works for Python 2 and Python 3. But it returns abytes()
string in Python 3. But that's reasonable for an ASCII string. -
Jona over 7 yearsBest solution for me (works with python 3) as it even accepts spaces :
bytearray.fromhex("70 61 75 6C").decode()
-
grambo over 6 yearsbytearray.fromhex("70e4756c").decode(encoding="Latin1") 'päul' For those of us playing in binary, the extended characters choke on the default utf-8 decode, other than that, this is the most portable answer I see! Thanks!
-
tripleee over 4 yearsOf course you have to know the actual encoding of the data if it is to be interpreted as text. Using
'latin-1'
will get rid of any errors but may well produce complete gibberish if the text is not actually Latin-1. -
rjferguson over 4 yearsThis works fine without the spaces as well, and works fine in python3 with print().
-
Admin almost 4 yearsYes, I put it on purpose to make it easier to see. Let me update the answer with Python 3 as well.
-
xuiqzy over 3 yearsIn the interpreter, even the
repr
of thebytearray
that is returned without.decode()
is human readable, so for quickly checking something, you might get away without the.decode()
. -
Martijn Pieters almost 3 yearsThis is no different from
bytes.fromhex()
orbytearray.fromhex()
. For both these types,.fromhex()
is a classmethod. -
maxschlepzig over 2 yearsor better
bytes.fromhex("7061756c").decode()
since you don't need a mutable array and it's less to type. -
Admin over 2 yearsAs it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
-
Ulf Gjerdingen almost 2 yearswhy can't i convert the value
9c
?