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

python - How to convert hex str into int array - Stack Overflow
I have strings of hex, for exemple '01ff6fee32785e366f710df10cc542B4' and I am trying to convert them (efficiently) into an int array 2 characters by 2 characters like [1,255,...]. I tried c = ' More on stackoverflow.com
🌐 stackoverflow.com
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
How to convert a string to array and convert each of the hex value in array to decimal in python? - Stack Overflow
I want the following steps to be implemented in python 1) string 7f33117cf266a525 2) uppercase 7F33117CF266A525 3) Put it in an array [7F,33,11,7C,F2,66,A5,25] 4) convert it to binary[127,5... More on stackoverflow.com
🌐 stackoverflow.com
arrays - How to convert hexadecimal string to bytes in Python? - Stack Overflow
I have a long Hex string that represents a series of values of different types. I need to convert this Hex String into bytes or bytearray so that I can extract each value from the raw data. How can... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Python Forum
python-forum.io › thread-3058.html
string to hex and back again
May 16, 2021 - i want to convert a string to hexadecimal 'Skaperen' -> '536b61706572656e' 'Python' -> '507974686f6e' and back again '536b61706572656E' -> 'Skaperen' '507974686f6E' -> 'Python' what is the best and most Pythonic way to do this not involving singl...
🌐
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.
🌐
Programming Idioms
programming-idioms.org › idiom › 176 › hex-string-to-byte-array › 2614 › python
Hex string to byte array, in Python
Bytes to hex string · Parse binary digits · Binary digits to byte array · Encode string into UTF-8 bytes · Cheatsheets · Issues · Report a bug · × · Python · a = bytearray.fromhex(s) C ·
Find elsewhere
🌐
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › python-convert-bytearray-to-hexadecimal-string
Convert Bytearray to Hexadecimal String - Python - GeeksforGeeks
April 19, 2025 - In hexadecimal representation we 16 values to represent a number. Numbers 0-9 are expressed by digits 0-9 ... The goal here is to convert a string into bytes in Python. This is essential for working with binary data or when encoding strings for storage or transmission. For example, given the string "Hello", these methods can convert it into a byte representation like b'Hello'. Let’s explore different method ... To create a string array in Python, different methods can be used based on the requirement.
🌐
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.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-hex-string-to-integer-list
Python Hex String to Integer List - GeeksforGeeks
July 23, 2025 - Hexadecimal strings are commonly encountered when working with low-level programming, networking, or cryptographic applications. In Python, converting a hex string to an integer array or list is a frequent task.
🌐
pythontutorials
pythontutorials.net › blog › how-to-convert-string-to-byte-array-in-python
How to Convert a String to a Byte Array with Hex Equivalent Characters in Python: A Step-by-Step Guide — pythontutorials.net
Converting a string to a byte array with hex equivalent characters involves three core steps: encoding the string to bytes, converting those bytes to a hex string, and (optionally) reversing the process.
🌐
w3resource
w3resource.com › python-exercises › string › python-data-type-string-exercise-85.php
Python: Convert a given Bytearray to Hexadecimal string - w3resource
Write a Python program to implement a function that takes a bytearray and returns a lower-case hexadecimal string without prefixes.
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "python how-to's" › "how to convert string to hex in 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.
🌐
Stack Overflow
stackoverflow.com › questions › 79916804 › convert-string-array-to-transposed-binary-hex
python - Convert string array to transposed binary/hex - Stack Overflow
March 29, 2026 - Here's what I have written, which does work, but I'd like to do it the Pythonic way with the inline loop. # list2 contains the array of bits at this point list3 = [] for i in range(32): s = "" for j in range(15, -1, -1): s = s + list2[j][i] list3.append(s) # list3 has the final binary array, still need to convert to hex print(list3)
🌐
Finxter
blog.finxter.com › home › learn python blog › python hex string to integer array or list
Python Hex String to Integer Array or List - Be on the Right Side of Change
November 23, 2022 - You use slicing hex_str[i:i+k] to batch k digits at a time to a substring and convert it to an integer. You use the list append() method to add one integer at a time to the list of ints. You return a list of integers. If you want to return an array of integers, simply convert the list to a NumPy array using the np.array(lst) function call: