ascii2dec

Sean Ross sross at connectmail.carleton.ca
Thu Aug 21 14:13:58 EDT 2003


"Uwe Mayer" <merkosh at hadiko.de> wrote in message
news:bi0c3p$8qh$1 at news.rz.uni-karlsruhe.de...
> Uwe Mayer wrote:
> How to convert:
>
> length = '\x01\x00\x00\x00'
>
> to an integer


As Peter suggested, use struct.unpack with the appropriate endianness, e.g.

>>> import struct
>>> length = '\x01\x00\x00\x00'
>>> # convert 'length' to signed integer (big-endian)
>>> unpacked = struct.unpack('>i', length)[0]  # unpack returns a tuple
>>> unpacked
16777216
>>> # convert 'length' to signed integer (little-endian)
>>> unpacked = struct.unpack('i', length)[0]   # or,  fmt = '<i'
>>> unpacked
1

HTH
Sean






More information about the Python-list mailing list