[Python-talk] How to print a bunch o bytes in hex?

Kent Johnson kent37 at tds.net
Wed Sep 23 13:34:03 EDT 2009


On Wed, Sep 23, 2009 at 12:36 PM, Shawn O'Shea <shawn at eth0.net> wrote:
> I was pretty sure there was something the standard library that did this. I
> went to docs.python.org and in the search box entered 'hex'. There were a
> large number of results for the binascii module.
> http://docs.python.org/library/binascii.html
> I believe you want to look at this function:
> binascii.b2a_hex(data)

Aww, hexlify() is such a nicer name :-)
You can link directly to a function in the docs:
http://docs.python.org/library/binascii.html#binascii.hexlify

You can also use the 'string_escape' codec for a representation as a
Python string though hexlify() is probably what you want:
In [4]: s = '\x00\x89\xaf\xb9'

In [5]: print s.encode('string_escape')
\x00\x89\xaf\xb9

Kent


More information about the Python-talk mailing list