Convert Hex to Byte in Python

Convert Hex to Byte in Python

Rayven Esplanada Dec-10, 2020 Dec-03, 2020 Python Python Bytes Python Hex
  1. Initialize a Hexadecimal Value
  2. Use bytes.fromhex() to Convert Hex to Byte in Python
  3. Use the unhexlify Module to Convert a Hex to Byte in Python

This tutorial will introduce how to convert hexadecimal values into a byte literal in Python.

Hexadecimal or hex values is a representation of a number made up of 16 symbols instead of the usual 10 symbols in decimal values. The numbers from 0 until 9 and the characters a until e makes up the 16 hex symbols.

For example, the hexadecimal conversion for the number 1000 is 3E8.

Initialize a Hexadecimal Value

Let’s create a hexadecimal value using a string and convert the phrase A quick brown fox into a hex value using the function hexlify() that is a function in the binascii module.

Also, to convert a string into a hex, we would need to convert the string into a byte to be able to convert it into hex.

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’ve successfully converted a string into hex, let’s proceed on how to convert a hex into a byte.

Use bytes.fromhex() to Convert Hex to Byte in Python

The function bytes.fromhex() accepts a single hexadecimal value argument and converts it into a byte literal.

Taking the hex value from the previous result, use fromhex() to convert it into a byte literal.

hex_val = '4120717569636b2062726f776e20666f78'

print(bytes.fromhex(hex_val))

Output:

Byte value:  b'A quick brown fox'

The result will output the bytes literal, which is the phrase converted into a hex prefixed with the letter b to specify that the value is a byte literal.

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

The binascii Python module contains efficient utility functions for binary and ASCII operations. unhexlify() is a function within the binascii module that converts a hexadecimal value into a byte literal.

Let’s initialize a new example with special non-ASCII characters, which will then be converted into a hexadecimal value. The example will be the Greek translation of the phrase a quick brown fox.

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 hexadecimal values to bytes.

In this article, we’ve covered using fromhex() and binascii.unhexlify() to convert hexadecimal values to byte literals in Python. fromhex() is preferable if you don’t want added imports into your source code. Otherwise, both functions will provide the same output.

Related Article - Python Bytes

  • Convert Bytes to Int in Python 2.7 and 3.x
  • Convert Int to Bytes in Python 2 and Python 3
  • Convert Int to Binary in Python
  • Convert Bytes to String in Python 2 and Python 3
  • Convert String to Bytes in Python
  • B in Front of String in Python
  • Related Article - Python Hex

  • Bitwise XOR of Hex Numbers in Python
  • Convert Binary to Hex in Python
  • Convert HEX to RGB in Python
  • Convert Hex to ASCII in Python
  • String to Hex in Python
  • Convert Hexadecimal to Decimal in Python