how many bytes in an int
Grant Edwards
grante at visi.com
Mon Aug 9 10:36:14 EDT 2004
On 2004-08-09, Robin Becker <robin at SPAMREMOVEjessikat.fsnet.co.uk> wrote:
> 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.
Like the man said, "struct" doesn't convert to-from integers of
specified byte lengths. All it has are the C types "int"
"long" "long long", etc. There is no portable way using struct
to request a 4-byte integer.
> >>> 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
Hopefully that can be fixed? Python integer objects seem to
get more difficult to work with every year. ;) A few weeks
back, somebody posted code for fixed length Python integer
objects.
--
Grant Edwards grante Yow! YOU PICKED KARL
at MALDEN'S NOSE!!
visi.com
More information about the Python-list
mailing list