How To/python/

Print string as hex code in python

To print string as sequence of hexadecimal codes of every character use this function:

def strAsHex(str):
    return ''.join(["\\x%02X" % ord(c) for c in str])

Example of use:

>>> print strAsHex('abcdef')
\x61\x62\x63\x64\x65\x66