Inconsistency in hex()

Bengt Richter bokr at oz.net
Wed Jul 13 01:07:51 CEST 2005


On Tue, 12 Jul 2005 21:17:07 +1000, Steven D'Aprano <steve at REMOVETHIScyber.com.au> wrote:

>hex() of an int appears to return lowercase hex digits, and hex() of a
>long uppercase.
>
>>>> hex(75)
>'0x4b'
>>>> hex(75*256**4)
>'0x4B00000000L'
>
>By accident or design? Apart from the aesthetic value that lowercase hex
>digits are ugly, should we care?
>
>It would also be nice if that trailing L would disappear.
>
 >>> '%010X'% 0x12345678
 '0012345678'
 >>> '%010X'% 75
 '000000004B'
 >>> '%010X'% (75*256**4)
 '4B00000000'
 >>> '%010X'% (-75*256**4)
 '-4B00000000'
 >>> '%010X'% (-75)
 '-00000004B'

I've ranted about the lack of a natural format for showing
the hex of a canonical twos-complement representation of a negative number,
but I guess I'll let it go with this mention ;-)

BTW, yeah, I know it's not so hard to write
 >>> '%010X'% (-75 &0xffffffffff)
 'FFFFFFFFB5'
 >>> '%010X'% (-75*256**4 &0xffffffffff)
 'B500000000'
or a helper or a str subclass that does __mod__ differently but that's not with the batteries ;-/
Regards,
Bengt Richter



More information about the Python-list mailing list