Python - How To Convert Hex to Byte in Python

ID : 131

viewed : 283

Tags : PythonPython BytesPython Hex

vote vote

97

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 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 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 HOW TO?