Long int to int

Gerhard Häring gh_pythonlist at gmx.de
Mon Feb 11 06:20:42 EST 2002


Le 11/02/02 à 10:40, Tim Howarth écrivit:
> I need to convert some long int values to normal ints.
> (4 byte words) [...]

>  |im    ---- ARM Powered ----

Assuming 32-bit hardware, like my Intel x86 and StrongARM processors :-)

0x81234567L is greater than sys.maxint, so you can't convert it to an
int directly. What *should* be possible, is to convert it to an int that
has the same bit pattern as an unsigned int (an unsigned int doesn't
exist in Python, normally).

Could it be that you need this to interface some external library or
file format? Maybe the struct module can help you?

For creating an equivalent bit representation, what about this:

    print struct.unpack("i", struct.pack("I", 0x81234567L))[0]

I currently don't remember enough, CS, but it should be easy to do
without the struct module, too.

Gerhard
-- 
This sig powered by Python!
Außentemperatur in München: 9.0 °C      Wind: 6.1 m/s




More information about the Python-list mailing list