Sunday, March 12, 2023
HomeSoftware EngineeringConvert Hex to Byte in Python

Convert Hex to Byte in Python


If you must 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 take a look at'.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 take a look at'

print(bytes.fromhex(hex_val))

Choice 3 – Utilizing unhexlify

import binascii
from binascii import unhexlify

str_val = 'This can be a take a look at'.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))

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments