how many bytes in an int

Grant Edwards grante at
Mon Aug 9 07:52:23 CEST 2004

On 2004-08-09, Grant Edwards <grante at> 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)

Grant Edwards                   grante             Yow!  Well, O.K. I'll
                                  at               compromise with my
                                 principles because of
                                                   EXISTENTIAL DESPAIR!

More information about the Python-list mailing list