Software Engineering

The right way to Convert Hex to Byte in Python

The right way to Convert Hex to Byte in Python
Written by admin


If it’s essential to convert Hex to Byte in Python, then you are able to do one of many following:

Choice 1 – Utilizing binascii

import binascii
str_val = 'This can be a check'.encode('utf-8')
hex_val = binascii.hexlify(str_val).decode('utf-8')

print(hex_val)

Choice 2 – Utilizing bytes.fromhex()

hex_val = 'This can be a check'

print(bytes.fromhex(hex_val))

Choice 3 – Utilizing unhexlify

import binascii
from binascii import unhexlify

str_val = 'This can be a check'.encode('utf-8')
hex_val = binascii.hexlify(str_val).decode('utf-8')

print('String worth: ', str_val.decode('utf-8'))
print('Hexadecimal: ', hex_val)
print('Byte worth: ', unhexlify(hex_val))

About the author

admin

Leave a Comment