ascii2dec

Peter Hansen peter at engcorp.com
Wed Aug 20 14:22:46 EDT 2003


Uwe Mayer wrote:
> 
> Uwe Mayer wrote:
> 
> > this must sound stupid to you, but I'm ages out of Python and I just can't
> > find a function to convert 4 bytes of binary data to an integer value:
> >
> > length=f.read(4)    # get length in binary
> > length=socket.htonl(length)    # swap bytes
> >
> > #convert 4 bytes to integer
> 
> Sorry, socket.htonl(...) already expects a number, so its:
> 
> > #convert 4 bytes to integer
> > length=socket.htonl(length)    # swap bytes
> 
> i.e.
> How to convert:
> 
> length = '\x01\x00\x00\x00'
> 
> to an integer

Use the struct module, with appropriate "endianism".

-Peter




More information about the Python-list mailing list