The int("0x31", 16) part is correct:

>>> int("0x31",16)
49

But to convert that to a character, you should use the chr(...) function instead:

>>> chr(49)
'1'

Putting both of them together (on the first letter):

>>> chr(int("0x53", 16))
'S'

And processing the whole list:

>>> [chr(int(i, 16)) for i in "0x53 0x48 0x41 0x53 0x48 0x49".split()]
['S', 'H', 'A', 'S', 'H', 'I']

And finally turning it into a string:

>>> hex_string = "0x53 0x48 0x41 0x53 0x48 0x49"
>>> ''.join(chr(int(i, 16)) for i in hex_string.split())
'SHASHI'

I hope this helps!

Answer from filbranden on Stack Overflow
🌐
Acme
acme.to › 2021 › 11 › 20 › python-convert-hex-to-ascii
Python convert hex to ascii – acme
import binascii def hex_to_ascii(hex_str): hex_str = hex_str.replace(' ', '').replace('0x', '').replace('\t', '').replace('\n', '') ascii_str = binascii.unhexlify(hex_str) return ascii_str hex_input = 'cd 73 6c 61 6e 64' ascii_output = hex_to_ascii(hex_input) print(format(ascii_output))
Discussions

How to store Hex and convert Hex to ASCII in Python? - Stack Overflow
My Command output is something like 0x53 0x48 0x41 0x53 0x48 0x49. Now i need to store this in a hex value and then convert it to ASCII as SHASHI. What i tried- I tried to store the values in hex... More on stackoverflow.com
🌐 stackoverflow.com
How do I convert a single character into its hex ASCII value in Python? - Stack Overflow
I am interested in taking in a single character. c = 'c' # for example hex_val_string = char_to_hex_string(c) print hex_val_string output: 63 What is the simplest way of going about this? Any More on stackoverflow.com
🌐 stackoverflow.com
Convert Hex to ascii
I am trying to convert a hex value to an ascii value. Any help would be appreciated. More on forum.inductiveautomation.com
🌐 forum.inductiveautomation.com
19
0
September 18, 2019
Converting string to ASCII HEX Python 3
I think the link where you got the code (presumably https://stackoverflow.com/questions/34975528/how-can-i-produce-a-hex-escaped-string-in-python-that-i-can-use-in-c ) just has a buggy example. >>> mystring = "Hello World" >>> print(''.join(r'\x{:02x}'.format(ord(c)) for c in mystring)) \x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64 I changed it to the print() function, and moved the ':' inside the string replacement field to the correct location. Edit: and here's the round-trip: >>> mystring = "Hello World" >>> a = ''.join(r'\x{:02x}'.format(ord(c)) for c in mystring) >>> a '\\x48\\x65\\x6c\\x6c\\x6f\\x20\\x57\\x6f\\x72\\x6c\\x64' >>> import ast >>> ast.literal_eval('"' + a + '"') 'Hello World' More on reddit.com
🌐 r/learnpython
6
1
November 22, 2020
🌐
Delft Stack
delftstack.com › home › howto › python › hex to ascii python
How to Convert Hex to ASCII in Python | Delft Stack
February 2, 2024 - The binascii module in Python provides methods for converting between binary data and ASCII text. The unhexlify() function converts hexadecimal strings to bytes.
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-hex-to-string-in-python
Convert Hex to String in Python - GeeksforGeeks
July 23, 2025 - In Python, converting hex to string is straightforward and useful for processing encoded data. This method splits the hex string into pairs of characters, converts each to an integer, then to a character (using ASCII values ) and then joins the result to form a string.
🌐
Finxter
blog.finxter.com › home › learn python blog › 4 pythonic ways to convert from hex to ascii
4 Pythonic Ways to Convert from HEX to ASCII - Be on the Right Side of Change
December 7, 2022 - The highlighted code takes in HEX values, converts them to a byte object using fromhex(), then converts them to an ASCII string by appending decode() to the end. If quote_a was output to the terminal, the following would display: To clean up the output, replace() is used on quote_a to replace ...
🌐
Javatpoint
javatpoint.com › how-to-convert-hex-to-ascii-in-python
How to convert Hex to ASCII in python - Javatpoint
How to convert Hex to ASCII in python with tutorial, tkinter, button, overview, canvas, frame, environment set-up, first python program, etc.
🌐
CodeSpeedy
codespeedy.com › home › convert hex to ascii in python
Convert Hex to ASCII in Python - CodeSpeedy
April 18, 2022 - hexa_string="0x48657920436F6465722E" slicing=hexa_string[2:] converting_hexa=bytes.fromhex(slicing) ascii_string=converting_hexa.decode() print(ascii_string) ... The output is as follows. Output: Hey Coder. Explanation: Initially, we declare a variable and initialize it to a hexadecimal value. In the second step we slice the given value, you can refer to How to slice a string in Python – Multiple way.
Find elsewhere
🌐
Bobby Hadz
bobbyhadz.com › blog › python-convert-hex-to-ascii
How to convert from HEX to ASCII in Python [5 Ways] | bobbyhadz
Use the bytearray.fromhex() method to get a new bytearray object that is initialized from the string of hex numbers. Use the decode() method to decode the bytearray. Optionally, set the encoding parameter to "ascii".
🌐
Python
docs.python.org › 3 › library › binascii.html
binascii — Convert between binary and ASCII
This function is the inverse of b2a_hex(). hexstr must contain an even number of hexadecimal digits (which can be upper or lower case), otherwise an Error exception is raised. Similar functionality (but more liberal towards whitespace) is also accessible using the bytes.fromhex() class method.
🌐
GitHub
gist.github.com › swang373 › c8290a522ce7cde573ac013729bb3f38
Hex to ASCII conversion in Python · GitHub
Hex to ASCII conversion in Python. GitHub Gist: instantly share code, notes, and snippets.
🌐
Inductive Automation
forum.inductiveautomation.com › ignition
Convert Hex to ascii - Ignition - Inductive Automation Forum
September 18, 2019 - I am trying to convert a hex value to an ascii value. Any help would be appreciated.
🌐
Reddit
reddit.com › r/learnpython › converting string to ascii hex python 3
r/learnpython on Reddit: Converting string to ASCII HEX Python 3
November 22, 2020 -

Hi all,

I've been struggling with this for an hour or so, I'm trying to take a string such as

'Hell' to '\x48\x65\x6c\x6c'

For what I'm doing, I require the \x before the ascii hex

This is a dated solution from StackOverflow, that doesn't work on Python3

>>> mystring = "Hello World"
>>> print ''.join(r'\x{02:x}'.format(ord(c)) for c in mystring)
\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64                

My new solution goes like this:

mystring = b'hello world'

#convert to hex
mystring = hexlify(mystring)
mystring = mystring.decode('ascii')
# hex, like this 68656c6c6f20776f726c64

#add in the \x in the right spots
mystring = '\\x'.join(mystring[i:i + 2] for i in range(-2, len(mystring), 2)) 
#looks like this at this point \x68\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64

#I need it back into the "b" format, so I encode it 
mystring=mystring.encode(ascii)
# This breaks everything, now it becomes:
# b'\\x68\\x65\\x6c\\x6c\\x6f\\x20\\x77\\x6f\\x72\\x6c\\x64'
# I specifically can't use the double backslashes.

Any ideas how to fix this or am I going about this all wrong? Thanks

🌐
GeeksforGeeks
geeksforgeeks.org › python › python-hex-function
hex() function in Python - GeeksforGeeks
July 11, 2025 - "0x" to indicate that it's in base-16. Example: ... Explanation: hex() function takes that number and converts it into its hexadecimal representation, which is 0xff. The result is stored in res.
🌐
Python Forum
python-forum.io › thread-194.html
convert hex encoded string to ASCII
September 29, 2016 - given a string of hexadecimal characters that represent ASCII characters, i want to convert it to those ASCII characters. for example: '707974686f6e2d666f72756d2e696f' -> 'python-forum.io' in python 2 i can do .decode('hex') but this is gone in ...