how many bytes in an int
Robin Becker
robin at SPAMREMOVEjessikat.fsnet.co.uk
Mon Aug 9 03:49:46 EDT 2004
Grant Edwards wrote:
> On 2004-08-09, Grant Edwards <grante at visi.com> wrote:
>
>
>>The struct module is the only thing I know about. If you're
>>worried about the "C" types in the struct module changing
>>underneat you, you could do a pure Python implimentation of
>>"python-int" to/from DWORD. It's utterly trivial and shouldn't
>>take more than one or two lines of code.
>
>
> def toLittleEndianDWORD(i):
> return ''.join(map(chr,[x&0xff for x in [i,i>>8,i>>16,i>>24]]))
>
> def fromLittleEndianDWORD(s):
> return ord(s[0]) + (ord(s[1])<<8) + (ord(s[2])<<16) + (ord(s[3])<<24)
>
this simple code will give warnings in 2.3, I think struct is really needed.
>>> toLittleEndianDWORD(-1)
'\xff\xff\xff\xff'
>>> fromLittleEndianDWORD(toLittleEndianDWORD(-1))
__main__:2: FutureWarning: x<<y losing bits or changing sign will return
a long in Python 2.4 and up
-1
>>>
--
Robin Becker
More information about the Python-list
mailing list