A slightly simpler solution (python 2 only):

>>> "7061756c".decode("hex")
'paul'
Answer from cjm 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) ascii_output = ''.join(chr(i) for i in ascii_output) print(format(ascii_output))
Discussions

Converting all chars in a string to ascii hex in python - Stack Overflow
Just looking for python code that can turn all chars from a normal string (all English alphabetic letters) to ascii hex in python. I'm not sure if I'm asking this in the wrong way because I've been More on stackoverflow.com
🌐 stackoverflow.com
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
Text to ASCII HEX via python?
Is there a python function that works in TD that will convert a whole string of text into ascii hex (to send with sendExclusive via the MIDI out chop) that is more efficient than just iterating the “ord()” function across each character of the string? Neither of the following seem to work ... More on forum.derivative.ca
🌐 forum.derivative.ca
0
0
December 9, 2015
How to convert a full ascii string to hex in python? - Stack Overflow
I have this string: string = '{'id':'other_aud1_aud2','kW':15}' And, simply put, I would like my string to turn into an hex string like this:' More on stackoverflow.com
🌐 stackoverflow.com
🌐
Delft Stack
delftstack.com › home › howto › python › hex to ascii python
How to Convert Hex to ASCII in Python | Delft Stack
February 2, 2024 - Suppose we have a string written in hexadecimal form, 68656c6c6f, and we want to convert it into an ASCII string, which will be hello (in ASCII code h = 68, e = 65, l = 6c and o = 6f). The string.decode(encoding, error) method in Python 2 takes an encoded string as input and decodes it using the character encoding specified in the encoding argument.
🌐
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 ...
🌐
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 the semi-colon with a newline and hyphen. The result saves to quote. ... Much better! ⭐ Recommended Tutorial: How to Decode a Hex String in Python?
🌐
YouTube
youtube.com › techniquecoding
Convert Hex String to ASCII String in Python||Python programming - YouTube
The string is written in hexadecimal form “0x68656c6c6f” and we want to convert it into an ASCII character string which will be hello as h is equal to 68 in ...
Published   September 30, 2022
Views   1K
Find elsewhere
🌐
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

🌐
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 (accepting only text string arguments, but more liberal towards whitespace) is also accessible using the bytes.fromhex() class method.
🌐
Know Program
knowprogram.com › home › convert hex string to ascii string in python
Convert Hex String to ASCII String in Python - Know Program
May 13, 2021 - Then, convert hexadecimal value string to their corresponding ASCII format string and extract all characters. Finally, the ASCII string will be displayed on the screen. The bytes.fromhex() function convert hex to the byte in python.
🌐
CodeSpeedy
codespeedy.com › home › convert hex to ascii in python
Convert Hex to ASCII in Python - CodeSpeedy
April 18, 2022 - 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. For the rest of the conversion, methods bytes.fromhex() and decode() are used. In the output, Ascii value is displayed.
🌐
TouchDesigner
forum.derivative.ca › beginners
Text to ASCII HEX via python? - Beginners - TouchDesigner forum
December 9, 2015 - Is there a python function that works in TD that will convert a whole string of text into ascii hex (to send with sendExclusive via the MIDI out chop) that is more efficient than just iterating the “ord()” function across each character of the string? Neither of the following seem to work ...
Top answer
1 of 2
6

You can encode()the string:

string = "{'id':'other_aud1_aud2','kW':15}"
h = hexlify(string.encode())
print(h.decode())
# 7b276964273a276f746865725f617564315f61756432272c276b57273a31357d

s = unhexlify(hex).decode()
print(s) 
# {'id':'other_aud1_aud2','kW':15}
2 of 2
1

The tricky bit here is that a Python 3 string is a sequence of Unicode characters, which is not the same as a sequence of ASCII characters.

  • In Python2, the str type and the bytes type are synonyms, and there is a separate type, unicode, that represents a sequence of Unicode characters. This makes it something of a mystery, if you have a string: is it a sequence of bytes, or is it a sequence of characters in some character-set?

  • In Python3, str now means unicode and we use bytes for what used to be str. Given a string—a sequence of Unicode characters—we use encode to convert it to some byte-sequence that can represent it, if there is such a sequence:

    >>> 'hello'.encode('ascii')
    b'hello'
    >>> 'sch\N{latin small letter o with diaeresis}n'
    'schön'
    >>> 'sch\N{latin small letter o with diaeresis}n'.encode('utf-8')
    b'sch\xc3\xb6n'
    

    but:

    >>> 'sch\N{latin small letter o with diaeresis}n'.encode('ascii')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    UnicodeEncodeError: 'ascii' codec can't encode character '\xf6' in position 3: ordinal not in range(128)
    

Once you have the bytes object, you already know what to do. In Python2, if you have a str, you have a bytes object; in Python3, use .encode with your chosen encoding.

🌐
GeeksforGeeks
geeksforgeeks.org › dsa › convert-hexadecimal-value-string-ascii-value-string
Convert Hexadecimal value String to ASCII value String - GeeksforGeeks
July 11, 2025 - Extract first two characters from the hexadecimal string taken as input. Convert it into base 16 integer. Cast this integer to character which is ASCII equivalent of 2 char hex.
🌐
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.
🌐
Coding
coding.tools › hex-to-ascii
Hex to ASCII String Converter Online Tool - Coding.Tools
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.encode()) return ascii_str hex_input = '54 68 69 73 20 69 73 20 61 6e 20 65 78 61 6d 70 6c 65 2e' ascii_output = hex_to_ascii(hex_input) print('ascii result is:{0}'.format(ascii_output)) ------------------- ascii result is:b'This is an example.' public class NumberConvertManager { public static String hex_to_ascii(String hex_str) { hex_str = hex_str.replace(" ", "").replace("0x", "").replace("\\x", ""); StringBuilder ascii_st
🌐
ActiveState
code.activestate.com › recipes › 496969-convert-string-to-hex
Convert string to hex « Python recipes « ActiveState Code
>>> "hello".encode("hex") '68656c6c6f' >>> "68656c6c6f".decode("hex") 'hello' >>> ... Nevermind, I should've read the original post more closely. As the other commenter said, str.encode() and str.decode() do the same thing as your code. ... Even though modern Python implementations have .encode and .decode, old versions (pre v2) don't.