String --> int

Tyler Eaves tyler at tylereaves.com
Thu Jun 21 07:25:48 EDT 2001


On Thu, 21 Jun 2001 13:17:55 +0200, "Romain Guy" <romain.guy at jext.org>
wrote:

>    Assuming fsock is a File objet, when I do:
>var = fsock.read(4)
>I get a String. I'd like to convert it into an integer. I haven't found any
>method for this so I built this one:
>
>def _buildInteger(bytes):
>  if len(bytes) != 4:
>    return -1
>  else:
>    return ord(bytes[0]) | ord(bytes[1]) << 8 | ord(bytes[2]) << 16 |
>ord(bytes[3]) << 24
>
>It works perfectly but I'd like to use one internal to Python if possible...

I hare to tell ya this, but try int(strg)



More information about the Python-list mailing list