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.

Related Posts

  • Get variable name as String in Python
    01 October

    How to get variable name as String in Python

    Table of ContentsWhat are variable names in Python?How to get a variable name as a string in Python?In Python 2.xIn Python 3.x In Python, there are several ways to convert a string to a variable name, but the reverse process is not exactly utilized so often in the programming world. This article focuses on how […]

  • Remove commas from String in Python
    23 May

    Remove comma from String in Python

    Table of ContentsUsing replace() function to remove comma from string in PythonUsing re or RegEx package function to remove comma from string in Python 💡 Outline You can remove comma from string in python by using string’sreplace() method. Syntax: [crayon-61700ac047fc3958505611/] In this tutorial, we will take a sample string with a couple of commas in […]

  • Add character to String in Python
    21 May

    Add character to String in Python

    Table of ContentsUsing the + operatorUsing the join() functionUsing the % operatorUsing the format() functionUsing the f-strings Strings are considered as a sequence of characters in programming. In Python, string objects are considered immutable. What this means is that string objects cannot be changed once created, and a new string is created with a different […]

  • Python find number in String
    16 May

    Python find number in String

    Table of ContentsUse of re.findall() functionUse of List Comprehension, split() and isdigit() functionsUse of split() and append() functionsUse of Numbers from String Library Extracting specific letters, phrases, words, and some specific characters is a very generic thing to do in the world of programming. In all these tasks, extraction of numbers and their occurrences from […]

  • 13 April

    How to Format Float to 2 Decimal Places in Python?

    In this article you will learn - How to Format a Float With Two Decimal Places in Python?

  • 11 April

    Python Remove Newline from List

    Table of ContentsIntroduction✨ Method 1: Using the replace() method✨ Method 2: Using re.sub()✨ Method 3: Using strip()✨ Method 4: Slice the List Elements✨ Method 5: Using List Comprehension and Splitlines Function✨ Method 6: Using str.maketrans(x,y) and str.translate(map)Conclusion 🎯 Objective: In this article, we will discuss the methods to remove ‘\n’ or newline characters from a […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.