python hex string to hex code example

Example 1: hex to string python

>>> bytes.fromhex('HexValue').decode('utf-8')
'string'
>>> bytes.fromhex('7368616b6564').decode('utf-8')
'shaked'

Example 2: string to hex python

>>> s = 'The quick brown fox jumps over the lazy dog.'.encode('utf-8')
>>> s
b'The quick brown fox jumps over the lazy dog.'
>>> s.hex()
'54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f672e'

Example 3: python hex to bytes string

hexadecimal_string = "AB"
byte_array = bytearray.fromhex(hexadecimal_string)
print(byte_array)

Example 4: hex to string python

>>> b'\xde\xad\xbe\xef'.hex()
'deadbeef'