<div class="gmail_quote">On Sat, Dec 3, 2011 at 8:45 AM, Antoine Pitrou <span dir="ltr"><<a href="mailto:solipsis@pitrou.net">solipsis@pitrou.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
On Sat, 3 Dec 2011 14:31:51 +1000<br>
Nick Coghlan <<a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a>> wrote:<br>
><br>
> If you really want that (Why would you?) and so long as the numbers<br>
> aren't negative:<br>
><br>
> "0B{:b}".format(number)<br>
> "0O{:o}".format(number)<br>
><br>
> The only reason 'X' is provided for hexadecimal formatting is to<br>
> capitalize the letters that appear within the number itself.<br>
<br>
By the way, any reason why hex output represents negative number with a<br>
negative sign (instead of the more usual 2s-complement representation)?<br>
<br>
It's not too difficult to normalize by hand (e.g. add 2**32 if you know<br>
the number is a 32-bit one) but it always irks me that Python doesn't<br>
do it by default. I cannot think of a situation where the "sign" is<br>
relevant when printing a hex number: hex is about the raw binary<br>
representation of the number.<br></blockquote></div><br>This is because Python's integers are not limited to 32 bits or 64 bits. If you read PEP 237, you'll see that this was one of the hardest differences between ints and longs to be resolved. You'd have to include an infinite number of leading 'F' characters to format a negative long this way...<br clear="all">
<br>-- <br>--Guido van Rossum (<a href="http://python.org/~guido">python.org/~guido</a>)<br>