How can I convert from hex to plain ASCII in Python?
Note that, for example, I want to convert “0x7061756c” to “paul”.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
A slightly simpler solution:
>>> "7061756c".decode("hex")
'paul'
Method 2
No need to import any library:
>>> bytearray.fromhex("7061756c").decode()
'paul'
Method 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).
Method 4
In Python 2:
>>> "7061756c".decode("hex")
'paul'
In Python 3:
>>> bytes.fromhex('7061756c').decode('utf-8')
'paul'
Method 5
b''.fromhex('7061756c')
use it without delimiter
Method 6
Here’s my solution when working with hex integers and not hex strings:
def convert_hex_to_ascii(h):
chars_in_reverse = []
while h != 0x0:
chars_in_reverse.append(chr(h & 0xFF))
h = h >> 8
chars_in_reverse.reverse()
return ''.join(chars_in_reverse)
print convert_hex_to_ascii(0x7061756c)
Method 7
Tested in Python 3.3.2
There are many ways to accomplish this, here’s one of the shortest, using only python-provided stuff:
import base64 hex_data ='57696C6C20796F7520636F6E76657274207468697320484558205468696E6720696E746F20415343494920666F72206D653F2E202E202E202E506C656565656173652E2E2E212121' ascii_string = str(base64.b16decode(hex_data))[2:-1] print (ascii_string)
Of course, if you don’t want to import anything, you can always write your own code. Something very basic like this:
ascii_string = ''
x = 0
y = 2
l = len(hex_data)
while y <= l:
ascii_string += chr(int(hex_data[x:y], 16))
x += 2
y += 2
print (ascii_string)
Method 8
Alternatively, you can also do this …
Python 2 Interpreter
print "x70 x61 x75 x6c"
Example
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bdc8ced8cffdd1d4d3c8c5">[email protected]</a>:~# python Python 2.7.14+ (default, Mar 13 2018, 15:23:44) [GCC 7.3.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print "x70 x61 x75 x6c" p a u l >>> exit() <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="84f1f7e1f6c4e8edeaf1fc">[email protected]</a>:~#
or
Python 2 One-Liner
python -c 'print "x70 x61 x75 x6c"'
Example
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b5c0c6d0c7f5d9dcdbc0cd">[email protected]</a>:~# python -c 'print "x70 x61 x75 x6c"' p a u l <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="84f1f7e1f6c4e8edeaf1fc">[email protected]</a>:~#
Python 3 Interpreter
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0a7f796f784a6663647f72">[email protected]</a>:~$ python3
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("x70 x61 x75 x6c")
p a u l
>>> print("x70x61x75x6c")
paul
Python 3 One-Liner
python -c 'print("x70 x61 x75 x6c")'
Example
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7c090f190e3c1015120904">[email protected]</a>:~$ python -c 'print("x70 x61 x75 x6c")'
p a u l
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85f0f6e0f7c5e9ecebf0fd">[email protected]</a>:~$ python -c 'print("x70x61x75x6c")'
paul
Method 9
No need to import anything, Try this simple code with example how to convert any hex into string
python hexit.py
Hex it>>some string
736f6d6520737472696e67
python tohex.py
Input Hex>>736f6d6520737472696e67
some string
cat tohex.py
s=input("Input Hex>>")
b=bytes.fromhex(s)
print(b.decode())
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0