The hex() function converts a specified integer number into a hexadecimal string representation.

Use int(x, base) with 16 as base to convert the string x to an integer. Call hex(number) with the integer as number to convert it to hexadecimal.

hex_string = "0xAA"

"0x" also required

an_integer = int(hex_string, 16)

hex_value = hex(an_integer)

print(hex_value)

Output

0xaa

Answer from Thuấn Đào Minh on Stack Overflow
🌐
Delft Stack
delftstack.com › home › howto › python › str to hex python
How to Convert String to Hex in Python | Delft Stack
February 2, 2024 - The result of the encode() method is a sequence of bytes, which can be further processed using various methods. For example, the hex() function can be applied to convert each byte into its hexadecimal representation.
🌐
Linux Hint
linuxhint.com › string-to-hexadecimal-in-python
String to Hexadecimal in Python – Linux Hint
For that, we have to convert the string to byte using the method encode (), and then we can convert the string to hexadecimal as shown below: # string_to_hex_utf8.py # convert the string to the bytes str= 'linuxhint'.encode('utf-8') # print the converted string to bytes print(str) # convert ...
Discussions

String to HEX
I've tried .encode, binascii.hexlify, and a few other things. Ok, well, encode was the correct one, so do that. More on reddit.com
🌐 r/learnpython
7
5
September 2, 2024
How to convert character to hexadecimal and back?
I tried to code to convert string to hexdecimal and back for controle as follows: input = 'Україна' table = [] for item in input: table.append(item.encode('utf-8').hex()) output = '' for item in table: output += chr(int(item, 16)) print(output) To my surprise the output is total different ... More on discuss.python.org
🌐 discuss.python.org
6
0
March 22, 2023
python - How to convert a hex string to hex number - Stack Overflow
I want to convert a hex string (ex: 0xAD4) to hex number, then to add 0x200 to that number and again want to print that number in form of 0x as a string. i tried for the first step: str(int(str(i... More on stackoverflow.com
🌐 stackoverflow.com
converters - convert string to hex in python - Stack Overflow
Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams ... I have a script that calls a function that takes a hexadecimal number for an argument. The argument needs to the 0x prefix. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Python.org
discuss.python.org › python help
How to convert character to hexadecimal and back? - Python Help - Discussions on Python.org
March 22, 2023 - I tried to code to convert string to hexdecimal and back for controle as follows: input = 'Україна' table = [] for item in input: table.append(item.encode('utf-8').hex()) output = '' for item in table: output += chr…
🌐
Python Guides
pythonguides.com › convert-string-to-hexadecimal-in-python
How To Convert String To Hex In Python?
April 3, 2025 - The most simple approach to convert a string to hex in Python is to use the string’s encode() method to get a bytes representation, then apply the hex() method to convert those bytes to a hexadecimal string.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-hex
Python hex() | DigitalOcean
August 3, 2022 - From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.
Find elsewhere
🌐
CoenRaets
jsonviewer.ai › python-string-to-hex
Python String to Hex [Explained with Code Examples] - JSON Viewer
July 5, 2023 - Then, we transform this string variable to bytes by using the encode() method, and the outcome is saved in a variable named bytes. The bytes are then translated into a hexadecimal representation using the hex() method, and the output is saved in a variable named hexadecimal.
🌐
Python Forum
python-forum.io › thread-1715.html
how to convert a string to hex
January 22, 2017 - what is the best way to convert a string to hexadecimal? the purpose is to get the character codes to see what is being read in from a file. i already have command line tools that display the file in hexadecimal. i want to match that up with what...
🌐
GitHub
gist.github.com › fivepoint-0 › 69a1201dbdfba04b0e44c3d435ebf5f5
Python 3 - Convert String to Hex String · GitHub
Python 3 - Convert String to Hex String · Raw · toHex.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
ActiveState
code.activestate.com › recipes › 496969-convert-string-to-hex
Convert string to hex « Python recipes « ActiveState Code
toHex = lambda x:"".join([hex(ord(c))[2:].zfill(2) for c in x]) The builtin string-method "join" joins every element of the list to the string by re-using the string.
🌐
Reddit
reddit.com › r/learnpython › converting string to hex in python 3.5
r/learnpython on Reddit: Converting string to hex in Python 3.5
September 5, 2016 - How can I accomplish this in Python 3.5? ... I tried using binascii.unhexlify but then I need that value as a string so the string representation of 0xafbfcfdf. Is there a way to get that? ... let me get this straight: you have a legit hex string "afbfcfdf" and you just want 0x in front of ...
🌐
Hex to String
hextostring.com › post › pythonstrtohex
Python String to Hex: encode().hex() and Pitfalls | Hex to String Guides
July 22, 2026 - Python string → hex is: encode text to bytes, then render those bytes as hex digits.
🌐
Sling Academy
slingacademy.com › article › python-ways-to-convert-a-string-to-a-hexadecimal-value
Python: 3 ways to convert a string to a hexadecimal value - Sling Academy
May 27, 2023 - If you are using Python 3.5 or newer, you can use the encode() and the hex() methods to convert a given string to a hex value with a single line of code.
🌐
GeeksforGeeks
geeksforgeeks.org › python-hex-function
hex() function in Python - GeeksforGeeks
May 9, 2025 - hex() function in Python is used to convert an integer to its hexadecimal equivalent. It takes an integer as input and returns a string representing the number in hexadecimal format, starting with "0x" to indicate that it's in base-16.