Python 3.1.1 string to hex
In Python 3.5+, encode the string to bytes and use the hex()
method, returning a string.
s = "hello".encode("utf-8").hex()
s
# '68656c6c6f'
Optionally convert the string back to bytes:
b = bytes(s, "utf-8")
b
# b'68656c6c6f'
The hex
codec has been chucked in 3.x. Use binascii
instead:
>>> binascii.hexlify(b'hello')
b'68656c6c6f'