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
Discussions

Converting string to hex in Python 3.5
I am having trouble converting a string of a 4-byte hex value into a hex value and appending it to a string. For example, I have a string "afbfcfdf"… More on reddit.com
🌐 r/learnpython
18
1
September 5, 2016
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
Problem parsing hex formatted as \x48\x45\x4c\x4c\x4f
A "\x48" is literally the same as "H". You don't have to do any conversion because it's already the same thing. It's just a different way of writing it. >>> "\x48" == "H" True More on reddit.com
🌐 r/learnpython
7
1
March 25, 2019
Need some help with converting hex to bytearray for long strings
then the byte array should look like this ['0xaa','0xbb','0xcc','0xdd','0xee','0xff','0x00','0x11','0x22','0x33', '0x44', '0x55','0x66','0x77','0x88','0x99'] Note that this is different to the examples you give down below - this is a list of strings, each representing a hex value. Whereas the bytearray examples in the config are arrays of bytes (provided as a list of integers written as hex literals). I assume you mean the latter, since that makes more sense for something called a byte array (especially if using python's bytearray type). For this, you can fairly easily convert the hex into a bytearray object by using the bytearray.fromhex method. Eg: >>> b = bytearray.fromhex("aabbccddeeff00112233445566778899") >>> b bytearray(b'\xaa\xbb\xcc\xdd\xee\xff\x00\x11"3DUfw\x88\x99') If you do want your list of strings, you could convert back fairly easily with: >>> [hex(byte) for byte in b] ['0xaa', '0xbb', '0xcc', '0xdd', '0xee', '0xff', '0x0', '0x11', '0x22', '0x33', '0x44', '0x55', '0x66', '0x77', '0x88', '0x99'] Though if you don't need the bytearray version, you could also construct it by string slicing the original hex string without the round-trip, though this way is probably still easier. More on reddit.com
🌐 r/learnpython
3
2
September 27, 2022
🌐
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 ... 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 ...
🌐
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.
🌐
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.
🌐
Linux Hint
linuxhint.com › string-to-hexadecimal-in-python
String to Hexadecimal in Python – Linux Hint
Line 10: We also print the value type to confirm that the string is now in the form of an integer. Line 12: We know the in-built hex(n) method accepts the integer value, converting the integer to the hexadecimal string. That’s why we need to convert the string to an integer to pass it into the hex () method.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-hex
Python hex() | DigitalOcean
August 3, 2022 - Technical tutorials, Q&A, events — This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
Find elsewhere
🌐
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.
🌐
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.
🌐
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...
🌐
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 - We see the cleartext jump in the interpreter, because Python's repr for bytes objects happens to use ASCII for most characters in ASCII range, because that was convenient in the old days when we pretended we could get away with confusing bytes with text. Converting between sequences of bytes <-> strings (i.e.
🌐
W3Schools
w3schools.com › python › ref_func_hex.asp
Python hex() Function
The hex() function converts the specified number into a hexadecimal value. The returned string always starts with the prefix 0x. ... Coding fundamentals as bite-sized lessons and challenges.
🌐
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 - string = "Sling Academy" hex_value = ''.join([format(ord(char), 'x') for char in string]) print(hex_value) Because we use the same input as other examples in this article, the output won’t be a new thing: ... The advantage of this technique is that can handle non-ASCII characters and is flexible and customizable. The trade-off is that it requires iterating through each character, which may be less efficient for extremely large strings. Next Article: Python: 4 Ways to Generate Random Strings
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-hex-function
hex() function in Python - GeeksforGeeks
July 11, 2025 - Explanation: hex(a) convert 987654321 to its hexadecimal representation. hex() function raises a TypeError if a non-integer (e.g., a float or string) is passed as an argument.
🌐
Vultr Docs
docs.vultr.com › python › built-in › hex()
Python hex() - Convert Integer to Hexadecimal
November 25, 2024 - This code demonstrates how to display only the hexadecimal digits by removing the '0x' prefix from the string. This is particularly useful when you need to print an integer as a hex string in Python without the 0x prefix.
🌐
GitHub
gist.github.com › fivepoint-0 › 69a1201dbdfba04b0e44c3d435ebf5f5
Python 3 - Convert String to Hex String · GitHub
Python 3 - Convert String to Hex String. GitHub Gist: instantly share code, notes, and snippets.
🌐
Finxter
blog.finxter.com › home › learn python blog › python print string as hex: converting text to hexadecimal format
Python Print String as Hex: Converting Text to Hexadecimal Format - Be on the Right Side of Change
January 16, 2024 - The hex() function takes an integer and returns its hexadecimal representation as a string, where Python 3’s output includes the prefix 0x, making it clear that the number is in hex format.
🌐
Online String Tools
onlinestringtools.com › convert-string-to-hexadecimal
Convert a String to Hexadecimal – Online String Tools
This example converts a simple string to its hexadecimal representation and pads each byte with space.
🌐
GitHub
gist.github.com › rmrfslashbin › 8f1c65c4090cc1fd116561d39417aa98
Convert a string to hex · GitHub
Convert a string to hex · Raw · str2hex.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. Learn more about bidirectional Unicode characters ·