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

Share:
561,540

Related videos on Youtube

JanusFox81
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, 2022

Comments

  • JanusFox81
    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
      JanusFox81 about 12 years
      I've tried a bunch of stuff I found here: docs.python.org/library/binascii.html
    • Vincent Savard
      Vincent Savard about 12 years
      With 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
      JanusFox81 about 12 years
      I 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
      JanusFox81 about 12 years
      None of them worked, because none of them returned 'paul'.
    • Admin
      Admin about 12 years
      Don't you mean "7-bit" ASCII? (Which is sort of silly because ASCII is only 7-bits.) A GUID is 128bits...
  • jfs
    jfs about 12 years
    there is no .decode('hex') on Python 3. .decode('hex') uses binascii.unhexlify() on Python 2.
  • cjm
    cjm about 12 years
    Thanks 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
    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
    Mark Evans almost 9 years
    codecs.decode("7061756c", "hex") works for Python 2 and Python 3. But it returns a bytes() string in Python 3. But that's reasonable for an ASCII string.
  • Jona
    Jona over 7 years
    Best solution for me (works with python 3) as it even accepts spaces : bytearray.fromhex("70 61 75 6C").decode()
  • grambo
    grambo over 6 years
    bytearray.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
    tripleee over 4 years
    Of 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
    rjferguson over 4 years
    This works fine without the spaces as well, and works fine in python3 with print().
  • Admin
    Admin almost 4 years
    Yes, I put it on purpose to make it easier to see. Let me update the answer with Python 3 as well.
  • xuiqzy
    xuiqzy over 3 years
    In the interpreter, even the repr of the bytearray that is returned without .decode() is human readable, so for quickly checking something, you might get away without the .decode().
  • Martijn Pieters
    Martijn Pieters almost 3 years
    This is no different from bytes.fromhex() or bytearray.fromhex(). For both these types, .fromhex() is a classmethod.
  • maxschlepzig
    maxschlepzig over 2 years
    or better bytes.fromhex("7061756c").decode() since you don't need a mutable array and it's less to type.
  • Admin
    Admin over 2 years
    As 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
    Ulf Gjerdingen almost 2 years
    why can't i convert the value 9c ?