[Python-ideas] Negative hexes

Antoine Pitrou solipsis at pitrou.net
Sat Dec 3 17:45:00 CET 2011


On Sat, 3 Dec 2011 14:31:51 +1000
Nick Coghlan <ncoghlan at gmail.com> wrote:
> 
> If you really want that (Why would you?) and so long as the numbers
> aren't negative:
> 
>     "0B{:b}".format(number)
>     "0O{:o}".format(number)
> 
> The only reason 'X' is provided for hexadecimal formatting is to
> capitalize the letters that appear within the number itself.

By the way, any reason why hex output represents negative number with a
negative sign (instead of the more usual 2s-complement representation)?

It's not too difficult to normalize by hand (e.g. add 2**32 if you know
the number is a 32-bit one) but it always irks me that Python doesn't
do it by default. I cannot think of a situation where the "sign" is
relevant when printing a hex number: hex is about the raw binary
representation of the number.

Regards

Antoine.





More information about the Python-ideas mailing list