How to Convert Bytearray to Hexadecimal String using Python?


What is Hexadecimal String?

A hexadecimal string is a textual representation of data in the hexadecimal number system. In this system the numbers are represented using a base-16 notation which means in the combination of digits 0 to 9 and the letters A to F. Each hexadecimal digit corresponds to a 4-bit binary sequence which allowing compact representation of binary data.

In a hexadecimal string each character represents a nibble i.e. 4 bits of data. Two hexadecimal characters together form a byte i.e. 8 bits. Hexadecimal strings are commonly used to represent binary data such as byte sequences, memory addresses, or cryptographic keys in a more human-readable format.

For example let’s consider the following is a hexadecimal string.

1A3F6D

In the above string each character (1, A, 3, F, 6, D) represents a nibble and two characters together (1A, 3F, 6D) represent a byte.

Python provides built-in functions and methods to work with hexadecimal strings. The `hex()` function in python can convert integers to hexadecimal strings and the `binascii.hexlify()` function can convert binary data to a hexadecimal string.

What is Bytearray?

In Python `bytearray` is a mutable sequence of bytes. It is a built-in data type that represents a sequence of integers in which each will be in the range 0 to 255 and can be modified after creation. These are useful for working with binary data such as images, audio files, network packets and other data that consists of raw bytes.

The below are the key characteristics of a bytearrays. Let’s see them one by one.

Mutable

Unlike a regular string which is immutable, a bytearray can be modified after it is created. We can change individual byte values or manipulate the entire bytearray.

Sequence Type

Bytearray is a sequence type which means it supports indexing, slicing, iteration, and all other common sequence operations available in Python.

Range of Values

Each element in a bytearray is an integer in the range of 0 to 255. This corresponds to the possible values of a single byte i.e. 8 bits in binary representation.

Binary Data Handling

Bytearrays are particularly useful for handling binary data such as reading and writing data from files, working with network packets, and implementing cryptographic algorithms.

Similarity to Lists

In many ways bytearrays behave similarly to lists in Python but they are specialized for dealing with bytes rather than generic objects.

In Python there are multiple approaches to convert a bytearray to a hexadecimal string. In this article let’s go through each approach in detail with an example.

Using the `binascii` Module

The `binascii` module in Python provides several utility functions for converting binary data to different representations including hexadecimal. The `binascii.hexlify()` function can be used to convert a bytearray to a hexadecimal string.

Example

byte_array = bytearray(b'Hello, world!')
import binascii
hex_string = binascii.hexlify(byte_array).decode('utf-8')
print("The conversion of bytearray to hexadecimal string :",hex_string)

Output

The conversion of bytearray to hexadecimal string : 48656c6c6f2c20776f726c6421

Using List Comprehension

We can convert each byte in the bytearray to its hexadecimal representation by using the list comprehension and then join the results to create a hexadecimal string.

Example

byte_array = bytearray(b'Welcome to tutorialspoint')
hex_string = ''.join([format(byte, '02x') for byte in byte_array])
print("The conversion of bytearray to hexadecimal string :",hex_string)

Output

The conversion of bytearray to hexadecimal string :
57656c636f6d6520746f207475746f7269616c73706f696e74

Using the `hex()` Function

The `hex()` function can be used directly on each byte of the bytearray to convert it to a hexadecimal string. This takes the bytearray as the input parameter.

Example

byte_array = bytearray(b'Welcome to tutorialspoint,have a happy learning')
hex_string = ''.join(hex(byte)[2:].zfill(2) for byte in byte_array)
print("The conversion of bytearray to hexadecimal string :",hex_string)

Output

The conversion of bytearray to hexadecimal string :
57656c636f6d6520746f207475746f7269616c73706f696e742c686176652061206861707079206
c6561726e696e67

All three approaches will give us the same result of a hexadecimal string representation of the given bytearray. We can choose any of these approaches based on our preference, coding style, or specific requirements of our project. The `binascii.hexlify()` approach is more specific to converting binary data to hexadecimal while the other two methods provide more general-purpose solutions for converting bytes to hexadecimal strings.

Updated on: 03-Jan-2024

183 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements