0

given hex, and formed bytes

looking for bytes to string


def hex_to_b:
    return bytes.fromhex('abc')
#now you have a byte type string
#looking to decode back to the string

#failure of transformation
hex_to_b().decode(encoding="utf-8",errors="strict")

1 Answers1

1

To convert bytes to hex, and the other way round, use built-in binascii module.

https://docs.python.org/3/library/binascii.html

Example:

>>> from binascii import hexlify, unhexlify
>>> unhexlify('deadbeef')
b'\xde\xad\xbe\xef'
>>> hexlify(b'\xde\xad\xbe\xef').decode()
'deadbeef'

Make sure a valid hex-string is passed.

Sazzy
  • 1,924
  • 3
  • 19
  • 27