Since Python 3.5 this is finally no longer awkward:

>>> b'\xde\xad\xbe\xef'.hex()
'deadbeef'

and reverse:

>>> bytes.fromhex('deadbeef')
b'\xde\xad\xbe\xef'

works also with the mutable bytearray type.

Reference: https://docs.python.org/3/library/stdtypes.html#bytes.hex

Answer from Felix Weis on Stack Overflow
🌐
AskPython
askpython.com › python › examples › convert-bytes-to-hex-strings
4 Handy Ways to Convert Bytes to Hex Strings in Python 3 - AskPython
May 16, 2023 - Bytes are eight bits long, they might represent numbers, digits, strings and letters. In Python, a bit is represented by adding a lowercase ‘b’ prefix to a string. A hex string, or a hexadecimal string, combines digits from 0 to 9 and letters from A to F to represent numbers in base 16, a number system widely used in computing.
Discussions

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
arrays - How to convert hexadecimal string to bytes in Python? - Stack Overflow
It returns a bytearray and it reads hex strings with or without space separator. ... Eric O. Lebigot · Eric O. Lebigot Over a year ago · This works in Python 3, whereas hex_string.decode("hex") does not. More on stackoverflow.com
🌐 stackoverflow.com
Python: How to convert a string containing hex bytes to a hex string - Stack Overflow
I'm thinking binascii is the module I'm looking for, but I can't quite seem to get the exact results for which I am looking. Here's what I want to do. I want to convert: >>> s = ' More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
Python Examples
pythonexamples.org › python-bytes-to-string
Python Bytes to String
In this tutorial, we will learn the syntax of bytes.decode() method, and how to use decode() method to convert or decode a python bytes to a string object. The syntax of bytes.decode() method is · bytes.decode(encoding) where encoding specifies how to decode the bytes sequence. decode() method ...
🌐
Medium
medium.com › @nawazmohtashim › convert-hex-to-string-in-python-a-comprehensive-guide-36d4f128cd9e
Convert Hex to String in Python: A Comprehensive Guide | by Mohd Mohtashim Nawaz | Medium
December 13, 2023 - hex_string = "48656c6c6f20576f726c64" ... to integers, and then using chr() to obtain the corresponding character. The bytes.fromhex() method is available in Python 3 and is specifically designed for converting space-separated hex ...
🌐
FavTutor
favtutor.com › blogs › hex-to-string-in-python
Convert Hex to String in Python | 6 Methods with Code
September 16, 2023 - In this example, we first define the hexadecimal string, then use `bytes.fromhex()` to create a bytes object, and finally, we decode it into a string using the UTF-8 encoding. Another way to convert a hexadecimal string to a string in Python is by using list comprehension.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-convert-bytearray-to-hexadecimal-string
Convert Bytearray to Hexadecimal String - Python - GeeksforGeeks
July 12, 2025 - All the hex values are joined into one continuous string, no separators, no prefix. binascii.hexlify() function converts bytes or bytearray into their hexadecimal representation, but returns the result as a bytes object, which you can decode ...
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python hex to string
Python Hex to String - Spark By {Examples}
May 21, 2024 - How to convert hex to string in Python? You can convert a hexadecimal string to a regular string using the built-in functions of Python in a simple
Find elsewhere
🌐
w3resource
w3resource.com › python-exercises › extended-data-types › python-extended-data-types-bytes-bytearrays-exercise-5.php
Python program to convert hexadecimal string to bytes
August 11, 2025 - Write a Python script to take a hex string input, convert it to bytes, and then encode the bytes back into a hex string to verify the conversion.
🌐
Programming Idioms
programming-idioms.org › idiom › 175 › bytes-to-hex-string › 2613 › python
Bytes to hex string, in Python
(lambda (bytes &aux (size (* 2 (length bytes)))) (let ((hex (make-array size :element-type 'character :fill-pointer 0))) (prog1 hex (with-output-to-string (o hex) (map () (lambda (byte) (format o "~2,'0x" byte)) bytes)))))
🌐
Reddit
reddit.com › r/learnpython › need some help with converting hex to bytearray for long strings
r/learnpython on Reddit: Need some help with converting hex to bytearray for long strings
September 27, 2022 -

Hi all,

I'm working with the API for The Things Network and I have a number of fields that expect HEX as their input. These are then matched to the equivalent values sent by the LoRaWAN devices for "shared key" authentication.

As an example, there is a field called "net_key" which is a 16 byte string, and at the moment I'm just generating a string via secrets.hex(16), however on the end devices I need to convert that into an array with each of the couplets split out, which then needs to be converted into a byte array

For example, if the hex call returns aabbccddeeff00112233445566778899, then the byte array should look like this:

['0xaa','0xbb','0xcc','0xdd','0xee','0xff','0x00','0x11','0x22','0x33', '0x44', '0x55','0x66','0x77','0x88','0x99']

Here's an example JSON payload for the API:

{
  "end_device": {
    "ids": {
      "device_id": "test-device"
    },
    "session": {
      "keys": {
        "app_s_key": {
          "key": "32079314899832864415563434004158"
        }
      },
      "dev_addr": "3d72ba69"
    }
  },
  "field_mask": {
    "paths": [
      "ids.device_id",
      "session.keys.app_s_key.key",
      "session.dev_addr"
    ]
  }
}

and here's what the device configuration should look like:

ttn_config = {
    "devaddr": bytearray([0x3d, 0x72, 0xba, 0x69]),
    "nwkey": bytearray([]),
    "app": bytearray([0x32, 0x07, 0x93, 0x14, 0x89, 0x98, 0x32, 0x86, 0x44, 0x15, 0x56, 0x34, 0x34, 0x00, 0x41, 0x58])
    "country": "EU",
}

Is there a an easy way that I'm missing to convert between the two formats?

🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-hex-to-string-in-python
Convert Hex to String in Python - GeeksforGeeks
July 23, 2025 - The codecs module can directly decode a hex string into bytes, which can then be converted to a string.
🌐
ActiveState
code.activestate.com › recipes › 510399-byte-to-hex-and-hex-to-byte-string-conversion
Byte to Hex and Hex to Byte String Conversion « Python recipes « ActiveState Code
def byte2int(bstr, width=32): """ Convert a byte string into a signed integer value of specified width. """ val = sum(ord(b) << 8*n for (n, b) in enumerate(reversed(bstr))) if val >= (1 << (width - 1)): val = val - (1 << width) return val ... But I dont know if these versions are faster than the original ones. ... You can also use python module binascii and function hexlify ( http://docs.python.org/library/binascii.html#binascii.hexlify )
🌐
Quora
quora.com › How-do-I-convert-hex-into-a-string-using-Python
How to convert hex into a string using Python - Quora
Answer (1 of 4): There are a few ways depending on the Python version you have on your computer. Python 2 [code]>>> "(HexValue)".decode("hex") 'string' >>> "7368616b6564".decode("hex") 'shaked' [/code]Python 2 + 3 [code]>>> bytearray.fromhex("HexValue").decode() 'string' >>> bytearray.fromh...
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-hex-string-to-bytes-in-python
Convert Hex String to Bytes in Python - GeeksforGeeks
July 23, 2025 - Converting a hexadecimal string to bytes in Python involves interpreting each pair of hexadecimal characters as a byte. For example, the hex string 0xABCD would be represented as two bytes: 0xAB and 0xCD.
🌐
w3resource
w3resource.com › python › python-bytes.php
Python Bytes, Bytearray
#the string must contain two hexadecimal digits per byte. x = '45678c6c56f205876f72c64' y = bytes.fromhex(x) ... #create a bytes object y = b'Python bytes' #generates a list of codes from the characters of bytes z = list(y) print(z)
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-bytearray-to-hexadecimal-string-using-python
How to Convert Bytearray to Hexadecimal String using Python?
January 3, 2024 - import binascii byte_array = bytearray(b'Hello, World!') hex_string = binascii.hexlify(byte_array).decode('utf-8') print("Hexadecimal string:", hex_string) ... byte_array = bytearray(b'Python') hex_string = ''.join([format(byte, '02x') for byte in byte_array]) print("Hexadecimal string:", hex_string)
🌐
DataCamp
datacamp.com › tutorial › bytes-to-string-python
How to Convert Bytes to String in Python | DataCamp
June 12, 2024 - To convert bytes to strings in Python, we can use the decode() method, specifying the appropriate encoding.
🌐
HCL GUVI
studytonight.com › python-howtos › how-to-convert-byte-to-hex-in-python
HCL GUVI | Learn to code in your native language
July 14, 2021 - HCL GUVI's Data Science Program was just fantastic. It covered statistics, machine learning, data visualization, and Python within its curriculum.