🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-hex-string-to-bytes-in-python
Convert Hex String to Bytes in Python - GeeksforGeeks
July 23, 2025 - You can convert a hexadecimal string into bytes using list comprehension in a single line by splitting the hex string into pairs of characters, converting each pair to its decimal equivalent and then converting the result into a bytes object.
Discussions

What's the correct way to convert bytes to a hex string in Python 3? - Stack Overflow
What's the correct way to convert bytes to a hex string in Python 3? I see claims of a bytes.hex method, bytes.decode codecs, and have tried other possible functions of least astonishment without ... More on stackoverflow.com
🌐 stackoverflow.com
arrays - How to convert hexadecimal string to bytes in Python? - Stack Overflow
Disadvantages: that is a byte string, not a hex string, so this is not an answer to the question. 2015-02-06T13:29:42.273Z+00:00 ... It is an answer to the 2nd part of the question "... so that I can shift each value out and convert it into its proper data type". 2019-05-06T16:02:47.767Z+00:00 ... Save this answer. ... Show activity on this post. You can use the Codecs module in the Python ... More on stackoverflow.com
🌐 stackoverflow.com
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
How to convert a large string having Hex digits into bytes?
Maybe I'm misunderstanding what you want to do, but if you just want to convert the hex string "abcd" into byte form then you can use the fromhex() function. a = "abcd" b = bytes.fromhex(a) print(b) >>> b'\xab\xcd' More on reddit.com
🌐 r/learnpython
12
9
February 28, 2026
🌐
Java2Blog
java2blog.com › home › python › python string › convert hex to bytes in python
Convert Hex to bytes in Python - Java2Blog
November 29, 2023 - Our aim is to explore different methods for this conversion, evaluate their performance, and pinpoint the scenarios where each method is most applicable. The bytes.fromhex() method is a straightforward approach provided by Python.
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "python how-to's" › "how to convert hex to byte in python"
How to Convert Hex to Byte in Python | Delft Stack
February 2, 2024 - The codecs.decode() function can also be used to convert a hexadecimal string into bytes. It’s part of Python’s codecs module, which provides various encoding and decoding functionalities for different data representations.
🌐
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 program that converts a hexadecimal string into a bytes sequence. ... def hex_str_to_bytes(hex_string): try: bytes_sequence = bytes.fromhex(hex_string) return bytes_sequence except ValueError as ve: print("Error:", ve) return None def main(): try: hex_string = "4861636b657220436f6465" bytes_result = hex_str_to_bytes(hex_string) if bytes_result is not None: print("Hexadecimal String:", hex_string) print("Bytes Sequence:", bytes_result) print("Decoded String:", bytes_result.decode("utf-8")) except Exception as e: print("An error occurred:", e) if __name__ == "__main__": main()
Find elsewhere
🌐
Python Guides
pythonguides.com › convert-hexadecimal-string-to-bytes-in-python
Convert a Hexadecimal String to Bytes in Python
September 5, 2025 - Using the codecs.decode() function to decode the hexadecimal string into bytes, specifying “hex” as the decoding format. We use List comprehension in Python to iterate over the hexadecimal string, taking two characters at a time (representing one byte), converting them to integers using ...
🌐
Finxter
blog.finxter.com › home › learn python blog › how to convert hex string to bytes in python?
How to Convert Hex String to Bytes in Python? - Be on the Right Side of Change
November 14, 2022 - To convert a hexadecimal string to a bytes object, pass the string as a first argument into bytes.fromhex(hex_string) method.
🌐
GeeksforGeeks
geeksforgeeks.org › python › bytes-fromhex-method-python
bytes.fromhex() Method - Python - GeeksforGeeks
June 18, 2026 - This method is commonly used when working with hexadecimal-encoded data and binary information. ... Explanation: bytes.fromhex(h) converts the hexadecimal string "48656c6c6f" into the bytes object b'Hello'.
🌐
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 - This method involves iterating through pairs of hex digits, converting them 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 ...
🌐
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?

🌐
Reddit
reddit.com › r/learnpython › how to convert a large string having hex digits into bytes?
r/learnpython on Reddit: How to convert a large string having Hex digits into bytes?
February 28, 2026 -

What I'm trying to do is convert a string like "abcd" into bytes that represent the hexadecimal of the string oxabcd. Python keeps telling me OverflowError: int too big to convert

The code is the following:

a = "abcd"

print(int(a, 16).to_bytes())

The error in full is:

Traceback (most recent call last):
  File "/home/repl109/main.py", line 6, in <module>
    print(int(a, 16).to_bytes())
        ^^^^^^^^^^^^^^^^^^^^^
OverflowError: int too big to convert

Anyone know a way to do this for large integers?

🌐
Programming Idioms
programming-idioms.org › idiom › 176 › hex-string-to-byte-array › 2614 › python
Hex string to byte array, in Python
public static byte[] hexToByteArray(String s) { int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16)); } return data; } ... int i, n = s.length(); byte a[] = new byte[n / 2]; for (i = 0; i < n; i = i + 2) a[i / 2] = (byte) fromHexDigits(s, i, i + 2); ...
🌐
Finxter
blog.finxter.com › home › learn python blog › python – hex string to bytearray
Python - Hex String to Bytearray - Be on the Right Side of Change
November 14, 2022 - A simple way to convert a hexadecimal string hex_string to a bytes type is to use the bytes.fromhex(hex_string) method.
🌐
FavTutor
favtutor.com › blogs › hex-to-string-in-python
Convert Hex to String in Python | 6 Methods with Code
September 16, 2023 - Here, we use `binascii.unhexlify()` to convert the hexadecimal string into a bytes object and then decode it into a string as we did in Method 1. The bytearray class in Python provides an alternative way to convert a hexadecimal string into bytes.
🌐
Finxter
blog.finxter.com › home › learn python blog › converting python hex strings to bytes: a comprehensive guide
Converting Python Hex Strings to Bytes: A Comprehensive Guide - Be on the Right Side of Change
February 23, 2024 - This approach is clean and efficient, as it doesn’t require any additional libraries or complex logic. Similarly, the bytearray.fromhex() function is another built-in Python method that creates a mutable bytearray object from a string of ...
🌐
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.
🌐
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
I don't want to start a 'one line bubble sort' kind of competition here but, if anyone can come up with a line comprehension implementation for the HexToByte function that is faster than the alternative, that would be interesting. ... remove strip means remove space separation. If you remove the space separation then you don't need the .strip(), this is true But then test 4 kind of sucks. Also, I like my hex byte output space separated.
🌐
Readthedocs
hexbytes.readthedocs.io › en › stable › hexbytes.html
Usage — HexBytes 2.0.0 documentation - Read the Docs
>>> from hexbytes import HexBytes ... back to the basic bytes type >>> bytes(hb) b"\x03\x08wf\xbfh\xe7\x86q\xd1\xeaCj\xe0\x87\xdat\xa1'a\xda\xc0 \x01\x1a\x9e\xdd\xc4\x90\x0b\xf1;" class hexbytes.main.HexBytes(val: bool | bytearray | bytes | int | str | memoryview) ... Thin wrapper around the python built-in bytes ...