Convert Hex to bytes in Python

Python hex to bytes

💡 Outline
You can use codecs’s decode() function to convert Hex to bytes in python. This method works for Python 2.x and Python 3.x
Here is an example:

In the world of programming, there are many conversions of data types that programmers have to perform that best suit their problem statements. One of these conversions is Hex to Bytes. The hexadecimal number is one such number system that is highly used in storing memory addresses, IP addresses, and more. And bytes is a binary serialization format that is represented by an 8-bit integer sequence. Bytes are fit for storing the system data as well as send it across the internet.

Hexadecimal values have a base of 16. To make a hexadecimal value, numbers from 0 to 9 and characters from a to e are used. Whereas, bytes on the other hand are represented by using the letter b as a prefix to any string in Python.

This tutorial will demonstrate some methods to convert hex to bytes in Python.

In this tutorial, we first start by creating a hexadecimal value of a string value which we will use in all the methods.

We will use the binascii module for doing this. The binascii module in Python provides the user with methods and functions for performing binary and ASCII operations in python. In the module, there is function called hexlify() function that is used to represent the hexadecimal form of any binary data. The resultant hexadecimal string is twice as long as the length of the input data.

The string that we will use in all the methods in this tutorial is Hello World. Now let’s see what is the Hexadecimal value of this string

Output:

Hexadecimal Value of the string: 48656c6c6f20576f726c64

Now that we have found the hexadecimal value of the string, we will see some methods to convert this Hexadecimal value to Bytes in Python.

Use codecs.decode() function to Convert Hexadecimal value to Byte in Python

This method works for both python 2.x and 3.x versions. You need to import codecs module for using this function.

Syntax

codecs‘s decode() functions takes object and encoding as input and decodes the object into specified encoding.

Since we need to convert hex String to bytes here, we need to pass encoding as hex_decode

Here is an example:

Output:


b’Hello World’

Use bytes.fromhex() function to Convert Hexadecimal value to Byte in Python

The fromHex() is a class method that creates bytes as an object from a hexadecimal string. For two hexadecimal strings, one byte is given in this method.

Example:

Output:

Byte form of the string= b’Hello World’

Note that, we have used the same hexadecimal string which we created in the starting.

Use the binascii Module to Convert Hexadecimal value to Byte in Python

In Python’s binascii module, there is a function called the unhexlify() function that is used in this method. This function helps in converting a hexadecimal string value into its byte form. We use the hexadecimal string as an argument of the function to convert it into bytes.

Example:

Output:

Byte form of the string: b’Hello World’

Here also, note that we have used the same hexadecimal string which we created in the starting.

That’s all about hex to bytes in Python.

Was this post helpful?

Related Posts

  • 19 May

    Remove First and Last Character of String in Python

    Table of ContentsHow to Remove First and Last Character of string in python?Remove First and Last Character of string Using For LoopRemove First and Last Character of string Using SlicingRemove First and Last word of string in PythonConclusionWas this post helpful? We use strings in python to handle text data. For processing the data, we […]

  • 19 May

    Remove HTML Tags From String in Python

    Table of ContentsRemove HTML tags from string in python Using Regular ExpressionsRemove HTML tags from string in python Using the lxml ModuleRemove HTML tags from string in python Using the Beautifulsoup ModuleConclusionWas this post helpful? While collecting data, we often need to process texts with HTML tags. In this article, we will discuss different ways […]

  • 19 May

    Remove xa0 from String in Python

    Table of ContentsHow to remove xa0 from string in python?Using the decode() function to remove xa0 from string in python.Using the re library to remove xa0 from string in python.Using the normalize() function from the unicodedata library to remove xa0 from string in python.Using the get_text() function from the BeautifulSoup library to remove xa0 from […]

  • 18 May

    Convert bool to String in Python

    Table of ContentsUsing the str() function to convert bool to string in Python.Using string formatting with the help of the % operator to convert bool to string in Python.Using string formatting with the help of the format() function to convert bool to string in Python.Using f-strings to convert bool to string in Python.Was this post […]

  • 18 May

    Convert UUID to String in Python

    Table of ContentsUUID in PythonWays to convert UUID to string in PythonUsing the str() function to convert UUID to String in PythonUsing the hex attribute to convert UUID to String in PythonUsing the urn attribute to convert UUID to String in PythonConclusionWas this post helpful? In this post, we will see how to convert UUID […]

  • 18 May

    How to Replace Word in String in Python

    Table of ContentsUsing the replace() Function to replace word in string in PythonUsing the re.Sub() Function to replace word in string in PythonUsing the re.subn() function to replace word in string in PythonConclusionWas this post helpful? In this tutorial, we will to how to replace word in string in Python. Strings represent a sequence of […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.