Q: Proper Radix Formatting of Longs?

Tom Vrankar no at spam.net
Thu Jun 3 00:19:50 EDT 1999


I use Python a lot for computer hardware testing and communications. It 
generally works great.

However, the (seemingly) wacky asymmetry between things like the % operator 
and string.atoi() when I must handle 32-bit fields such as address and data 
busses continues to drive me crazy. I often run into situations where I may 
need to handle a value like 0x8000_0000 and do math on it, while converting 
between strings and numbers. The % operator won't let me format longs, hex() 
has a fixed format that includes the sometimes inappropriate "0x" or "L", 
eval() considers 0x8000_0000 to be negative, and string.atoi() refuses to set 
the MSb of an integer unless it sees a dash at the beginning. So far, I 
special-case things a lot.

Is there a standard pythonic idiom for using 32-bit integers as if they were 
unsigned, so that 0x8000_0000 +0x8000_0000 =0, just like in C or my hardware 
ALU? Is there a standard pythonic way to format longs into arbitrary radices 
(sort of like an inverse string.atol)? Is there a secret handshake required to 
at least get string.atoi() to happily convert "0x8000_0000" symmetrically with 
"%x" %0x8000_0000? Yes, I know I could probably write some elaborate python 
functions to manually convert back and forth, but when handling megabytes, the 
performance would be unacceptable.

Any help is appreciated.

						twv@





More information about the Python-list mailing list