Elegant Way to Display Bytes as Hex Values in Python

Elegant code to print a hex string in python:

toHex = lambda x: "".join("{:02X}".format(ord(c)) for c in x)
print(toHex(b'\x03\xac23\n'))

or for Python3:

toHex = lambda x: "".join("{:02X}".format(c) for c in x)
print(toHex(b'\x03\xac23\n'))

Comments