Convert Byte to Hex in Python

  1. Initialize a Byte Literal in Python
  2. Use the hex() Method to Convert a Byte to Hex in Python
  3. Use the binascii Module to Convert a Byte to Hex in Python

This tutorial will introduce how to convert bytes into hexadecimal in Python.

The byte data type in Python is a sequence of bytes that can be stored on the disk as a variable, which can then be encoded and decoded. They are declared like a string but prefixed by the character b. Bytes accept special Unicode characters prefixed with \x.

Initialize a Byte Literal in Python

We will give an example of a byte literal, declare a string with special characters, and use the function encode('utf-8') to convert it to a byte literal.

byte_var = 'γιαούρτι - yogurt'.encode('utf-8')

print(byte_var)

Output:

b'\xce\xb3\xce\xb9\xce\xb1\xce\xbf\xcf\x8d\xcf\x81\xcf\x84\xce\xb9 - yogurt'

The output of encode() will result in a byte literal prefixed with the character b and the special characters converted into Unicode symbols.

Now the declaration of a byte is covered, let’s proceed with converting a byte into hex.

Use the hex() Method to Convert a Byte to Hex in Python

The hex() method introduced from Python 3.5 converts it into a hexadecimal string.

In this case, the argument will be of a byte data type to be converted into hex.

byte_var = 'γιαούρτι - yogurt'.encode('utf-8') 

print('Byte variable: ', byte_var)
print('Hexadecimal: ', byte_var.hex())

Output:

Byte variable:  b'\xce\xb3\xce\xb9\xce\xb1\xce\xbf\xcf\x8d\xcf\x81\xcf\x84\xce\xb9 - yogurt'
Hexadecimal:  ceb3ceb9ceb1cebfcf8dcf81cf84ceb9202d20796f67757274

Use the binascii Module to Convert a Byte to Hex in Python

The binascii Python module contains efficient utility functions for binary and ASCII operations.

Within this module, there is a function hexlify() that returns a hexadecimal value of the given argument, which is a binary value.

In this example, the argument will be the byte variable to be converted into hex.

import binascii

byte_var = 'γιαούρτι - yogurt'.encode('utf-8') 

print('Byte variable: ', byte_var)
print('Hexadecimal: ', binascii.hexlify(byte_var))

Output:

Byte variable:  b'\xce\xb3\xce\xb9\xce\xb1\xce\xbf\xcf\x8d\xcf\x81\xcf\x84\xce\xb9 - yogurt'
Hexadecimal:  b'ceb3ceb9ceb1cebfcf8dcf81cf84ceb9202d20796f67757274'

Take note that the return value of hexlify() returns a byte literal, unlike hex(), which returns a converted string.

If you want to convert the result into a string, use the function decode('utf-8').

import binascii

byte_var = 'γιαούρτι - yogurt'.encode('utf-8') 

print('Byte variable: ', byte_var)
print('Hexadecimal: ', '' + binascii.hexlify(byte_var).decode('utf-8'))

Output:

Byte variable:  b'\xce\xb3\xce\xb9\xce\xb1\xce\xbf\xcf\x8d\xcf\x81\xcf\x84\xce\xb9 - yogurt'
Hexadecimal:  ceb3ceb9ceb1cebfcf8dcf81cf84ceb9202d20796f67757274

Now the hexadecimal result is converted into a string from a byte literal.

In summary, we’ve covered 2 methods of converting a byte to hex in Python. The simplest way is to use the built-in function hex() to a byte literal. Alternatively, the hexlify() function from the binascii module can also be used to produce the same output.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python Bytes

  • B in Front of String in Python
  • Convert Hex to Byte in Python
  • Related Article - Python Hex

  • Convert Hex String to Int in Python
  • String to Hex in Python