JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Convert hex to bytes in Python

Author:JIYIK Last Updated:2025/05/05 Views:

Hexadecimal, often abbreviated to hex, uses 16 symbols (0-9, a-f) to represent values, in contrast to the decimal system's 10. For example, 1000 in decimal is 3E8 in hexadecimal.

Being proficient in dealing with hexadecimal is essential for programming tasks involving binary data, memory addresses, and low-level encodings. This tutorial will show you how to convert hexadecimal values ​​to bytes literals in Python.


Initialize hexadecimal value

Let's create a hexadecimal value using a string and use the function binasciiin the module to convert the phrase into a hexadecimal value.hexlify()A quick brown fox

To convert a string to hexadecimal, we first need to convert the string to bytes.

import binascii

str_val = "A quick brown fox".encode("utf-8")
hex_val = binascii.hexlify(str_val).decode("utf-8")

print(hex_val)

Output:

4120717569636b2062726f776e20666f78

Now, we have successfully converted a string to hex. Next, let's move on to converting the hex back to bytes.


bytes.fromhex()Python convert hex to bytes using function

bytes.fromhex()The method is used to convert a valid hexadecimal string into bytesan object. Its syntax is as follows:

bytes.fromhex(hex_string)
  • hex_string: This is a required parameter and represents the input hexadecimal string to be converted into a bytes literal.

This is bytes.fromhex()how the method works:

  1. Input Validation: This method first validates the input hex_stringto make sure it contains only valid hexadecimal characters (numbers 0-9 and lowercase or uppercase letters A-F). If the string contains any other characters, it will raise an ValueError.
  2. Parse pairs: This method processes the hexadecimal string two characters at a time, treating each pair of characters as a single hexadecimal byte. This means that for every two characters in the input string, bytesone byte in the result object is generated.
  3. Convert to bytes: Each pair of hexadecimal characters is converted to its binary equivalent.
  4. Building bytesan object: As the method processes pairs of hexadecimal characters, an bytesobject is built by appending the binary representation of each byte. This process continues until the entire input string has been processed.
  5. Return Value: Once the entire input string has been processed, the method returns bytesan object representing the bytes literal equivalent of the input hexadecimal string.

For example, we'll use the hexadecimal value from the previous result and fromhex()convert it to a bytes literal using .

hex_val = "4120717569636b2062726f776e20666f78"

print(bytes.fromhex(hex_val))

Output:

b'A quick brown fox'

Let's break down the example hex string 4120717569636b2062726f776e20666f78to better understand how it converts to a bytes literal:

  • 41becomes byte 65 ( 'A'ASCII value).
  • 20becomes byte 32 (the ASCII value of space).
  • 71becomes the byte 113 ( 'q'ASCII value).
  • 75becomes the byte 117 ( 'u'ASCII value).
  • 69becomes the byte 105 ( 'i'ASCII value).
  • 63becomes the byte 99 ( 'c'ASCII value).
  • 6bbecomes byte 107 ( 'k'ASCII value).
  • 20becomes byte 32 (the ASCII value of space).
  • 62becomes byte 98 ( 'b'ASCII value).
  • 72becomes the byte 114 ( 'r'ASCII value).
  • 6fbecomes the byte 111 ( 'o'ASCII value).
  • 77becomes 'w'the ASCII value of byte 119.
  • 6ebecomes the byte 110 ( 'n'ASCII value).
  • 20becomes byte 32 (the ASCII value of space).
  • 66becomes the byte 102 ( 'f'ASCII value).
  • 6fbecomes the byte 111 ( 'o'ASCII value).
  • 78becomes the byte 120 ( 'x'ASCII value).

When these bytes are combined together, you get the byte literal A quick brown fox, which is the final result.


binasciiPython convert hex to bytes using module

binasciiThe Python module contains efficient utility functions for binary and ASCII operations. In particular, unhexlify()there is binasciia function within the module that converts a hexadecimal value to a bytes literal.

The syntax of this function is as follows:

binascii.unhexlify(hex_string)
  • hex_string: This is a required parameter and represents the input hexadecimal string to be converted into a bytes literal.

Here is its detailed description:

  1. Input Validation: The method first validates hex_stringthe parameter to ensure that it contains only valid hexadecimal characters. Any invalid characters in the input string will result in binascii.Erroran exception.
  2. Hexadecimal Pair Conversion: This method then processes in pairs of characters hex_string. For example, the bytes 41representing Athe ASCII value of the integer value 65.
  3. Convert to Bytes: Hexadecimal byte pairs are converted to their binary representation. Each pair of characters is converted to the corresponding byte, where each character represents 4 bits.
  4. Build the literal: As the method processes each pair of characters, the resulting binary bytes are appended to form a complete byte literal. This process continues until the entire input string has been processed.
  5. Return Value: Once the entire string hex_stringhas been processed, unhexlify()the function returns the resulting bytes literal, represented as an bytesobject.

Let's initialize a new example with special non-ASCII characters and then convert them to hexadecimal values. The example will be A quick brown foxthe Greek translation of a phrase.

import binascii
from binascii import unhexlify

str_val = "Μια γρήγορη καφέ αλεπού".encode(
    "utf-8"
)  # A quick brown fox in Greek translation
hex_val = binascii.hexlify(str_val).decode("utf-8")

print("String value: ", str_val.decode("utf-8"))
print("Hexadecimal: ", hex_val)
print("Byte value: ", unhexlify(hex_val))

Output:

String value:  Μια γρήγορη καφέ αλεπού
Hexadecimal:  ce9cceb9ceb120ceb3cf81ceaeceb3cebfcf81ceb720cebaceb1cf86cead20ceb1cebbceb5cf80cebfcf8d
Byte value:  b'\xce\x9c\xce\xb9\xce\xb1 \xce\xb3\xcf\x81\xce\xae\xce\xb3\xce\xbf\xcf\x81\xce\xb7 \xce\xba\xce\xb1\xcf\x86\xce\xad \xce\xb1\xce\xbb\xce\xb5\xcf\x80\xce\xbf\xcf\x8d'

We have now successfully converted the hexadecimal value to bytes.


codecs.decode()Convert hex to bytes in Python using

codecs.decode()The function can also be used to convert hexadecimal strings to bytes. It is codecsa part of Python's encoding and decoding module that provides various encoding and decoding functions for different data representations.

Its syntax is as follows:

codecs.decode(data, encoding, errors="strict")
  • data: This is a required parameter representing the data to be decoded. In this case, it should be a hexadecimal string to be converted to a bytes literal.
  • encoding: This is also a required parameter and is used to specify the encoding used for decoding. When using hexadecimal strings, hexthe encoding should be used to indicate that the input string represents a hexadecimal value.
  • errors(Optional): This parameter specifies how to handle decoding errors, such as when the input string contains characters that are invalid for the specified encoding. Its default value is strict, but you can change it to ignoreor replaceto handle errors differently.

Here is a detailed description of the method when it is used to decode hexadecimal values:

  1. Input Validation: The method first checks datathe validity of the argument to ensure that it contains only valid hexadecimal characters. If the input string contains any non-hexadecimal characters, an error is raised with detailed information about the problem characters UnicodeDecodeError.
  2. Decoding process: codecs.decode()The function datainterprets the argument as a hexadecimal string and converts it into binary data. It processes the input string in character pairs, treating each pair of characters as a single hexadecimal byte.
  3. Encoding specification: The parameter is crucial in this context encoding. When you 'hex'specify as encoding, it tells Python to interpret the input string as a hexadecimal value and convert accordingly.
  4. Convert to bytes: As the method processes each pair of hexadecimal characters, it converts them to their binary representation. This conversion creates a bytes literal where each pair of characters corresponds to a single byte.
  5. Return value: codecs.decode()The method returns the generated bytes literal as an bytesobject representing the decoded binary data.

See the example below:

import codecs

hex_val = "4120717569636b2062726f776e20666f78"
byte_val = codecs.decode(hex_val, "hex")
print(byte_val)

This program decodes a string using codecs.decode()the function and the encoding parameter set to . This means that it will interpret the input string as hexadecimal values ​​and convert them into bytes.'hex'hex_val

Output:

b'A quick brown fox'

int()Convert hex to bytes in Python using list comprehension and function

This method involves using list comprehensions and int()functions to convert the hexadecimal string to bytes.

hex_val = "4120717569636b2062726f776e20666f78"
byte_val = bytes([int(hex_val[i : i + 2], 16) for i in range(0, len(hex_val), 2)])
print(byte_val)

Here, the code uses list comprehension to iterate over the hexadecimal string in pairs of two characters, convert each pair of characters to a hexadecimal integer, and create a list of these integers.

bytes()The constructor is then used to convert this list of integers into a bytes object.

Output:

b'A quick brown fox'

This method is particularly useful when we have a hex string of known structure and want to convert it to bytes efficiently. It provides control over the conversion process by allowing us to specify the encoding base (hexadecimal base 16 in this case).


in conclusion

In this article, we looked at some methods for converting hexadecimal values ​​to bytes literals in Python.

If you do not want to add additional imports to your source code, then fromhex()the function is preferred. Otherwise, you can choose the method that best suits your needs and coding style.

Previous:b in front of string in Python

Next: None

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

b in front of string in Python

Publish Date:2025/05/08 Views:53 Category:Python

This tutorial will discuss the statement in Python b" . b" Using the statement in Python b" The notation is used to specify strings in Python bytes . In contrast to regular strings with ASCII characters, bytes a string is an array of byte v

How to Convert Integer to Binary in Python

Publish Date:2025/05/08 Views:130 Category:Python

This tutorial explains how to convert an integer to binary in Python. This tutorial also lists some sample codes to illustrate different ways of converting from int to binary in Python. bin() Convert Int to Binary in Python using In Python,

How to convert an integer to bytes

Publish Date:2025/05/08 Views:77 Category:Python

Converting an integer int to a byte bytes is the inverse of bytes converting a byte to an integer . Most of the to methods int described in this article are the inverse of the to methods. int bytes bytes int Generic method for converting in

How to convert bytes to int in Python

Publish Date:2025/05/08 Views:111 Category:Python

Bytes The data type has a numerical range of 0~255 (0x00~0xFF). A byte has 8 bits of data, that's why its maximum value is 0xFF. In some cases, you need to convert a byte or byte array to an integer for further data processing. Let's see ho

How to Remove Punctuation from a String in Python

Publish Date:2025/05/08 Views:190 Category:Python

This tutorial discussed methods for removing punctuation from strings in Python. This is a particularly useful step when preprocessing and cleaning text data for NLP. string Remove punctuation from a string in Python using class methods We

How to Convert an Integer to a String in Python

Publish Date:2025/05/08 Views:69 Category:Python

This article will show you different ways to convert an integer to a string using Python code, such as str() the function and the f format method. str() Convert integer to string in Python using function We can use the inbuilt function in P

How to Replace Multiple Characters in a String in Python

Publish Date:2025/05/08 Views:97 Category:Python

This tutorial showed you how to replace multiple characters in a string in Python. Suppose we want to remove special characters from a string and replace them with spaces. The list of special characters to remove is !#$%^*() . Additionally,

How to remove the last character from a string in Python

Publish Date:2025/05/08 Views:141 Category:Python

A Python string is a combination of characters enclosed in double or single quotes. Python provides multiple functions to manipulate strings. This article will show you different ways to remove the last character and specific characters fro

How to Remove a Substring from a String in Python

Publish Date:2025/05/08 Views:182 Category:Python

This tutorial explains how to delete a substring in a string in Python. It will show you that strings cannot just be deleted, but only replaced. This tutorial also lists some sample codes to clarify the concepts, as the method has changed c

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial