get a list printed in hexadecimal notation
Bengt Richter
bokr at oz.net
Wed Jul 17 13:01:10 EDT 2002
On Wed, 17 Jul 2002 12:07:55 +0200, Oliver Eichler <oliver.eichler at dspsolutions.de> wrote:
>Hi
>
>Is it possible to print a mixed list with strings and numbers and having the
>numbers printed as hexadecimal notation?
>
>something like:
>
>>>>l = ('spam',10,15,'more spam')
>>>>print l
>('spam',0xA,0xF,'more spam')
>
>or similar...
>
>The only way I could think of is to write my own print function. Is there an
>easy way?
>
>>> def hexiseq(seq):
... if isinstance(seq,int): return hex(seq)
... if isinstance(seq,tuple): lb,rb = '(', ')'
... elif isinstance(seq,list): lb,rb = '[', ']'
... else: return repr(seq)
... ret = []
... for x in seq: ret.append(hexiseq(x))
... return lb + ', '.join(ret) + rb
...
>>> hexiseq(('spam',10,15,'more spam'))
"('spam', 0xa, 0xf, 'more spam')"
>>> hexiseq(('spam',10,15,'more spam',['nested',65535,'spam'],'spam',255))
"('spam', 0xa, 0xf, 'more spam', ['nested', 0xffff, 'spam'], 'spam', 0xff)"
>>> print hexiseq(('spam',10,15,'more spam'))
('spam', 0xa, 0xf, 'more spam')
>>> print hexiseq(('spam',10,15,'more spam',['nested',65535,'spam'],'spam',255))
('spam', 0xa, 0xf, 'more spam', ['nested', 0xffff, 'spam'], 'spam', 0xff)
Regards,
Bengt Richter
More information about the Python-list
mailing list