how to print out each single char from a string in HEX format?

Grant Edwards grante at visi.com
Tue Jun 5 17:19:19 EDT 2007


On 2007-06-05, Troels Thomsen <nej> wrote:

> with the printf inspired solution you can set the precision like this:
>
>>>> a = "12\n34"
>>>> for c in a:
> ...   print "%#04x" % ord(c),
> ...
> 0x31 0x32 0x0a 0x33 0x34

And if you just want to do the conversion w/o printing:

>>> a = "12\n34"

>>> ' '.join(('%#04x' % ord(c) for c in a))

'0x31 0x32 0x0a 0x33 0x34'

>>> ' '.join(('%02x' % ord(c) for c in a))

'31 32 0a 33 34'

-- 
Grant Edwards                   grante             Yow! Vote for ME -- I'm
                                  at               well-tapered, half-cocked,
                               visi.com            ill-conceived and
                                                   TAX-DEFERRED!



More information about the Python-list mailing list