python 如何 打印负数为16进制 无符号数?

Posted by abloz on December 30, 2011

如,想将-1打印成0xffffffff,结果是:

>>> a=-1
>>> hex(a)
'-0x1'
>>> print "%u"%a
-1
>>> print "%x"%a
-1
>>> print hex(a)
-0x1

没有办法将其打印成0xffffffff。打印和转化办法,可以定义hex2函数进行转换:

>>> print hex(a&0xffffffff)
0xffffffffL
>>> b=-2
>>> print "0x%08x"%(b&0xffffffff)
0xfffffffe

>>> def hex2(a):
...     return a>0 and hex(a) or hex(a&0xffffffff)
...
>>> hex2(a)
'0xffffffffL'
>>> hex2(234)
'0xea'
>>> hex2(b)
'0xfffffffeL'