String --> int

Uwe Schmitt schmitt at num.uni-sb.de
Thu Jun 21 08:15:21 EDT 2001


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...

in the module string, there's a method atoi() "ascii to integer"...

or do you want to convert a hex-string to its 10-base integer
representation ???

yours, uwe.

-- 
Uwe.Schmitt at num.uni-sb.de      Universität des Saarlandes
phone: +49 (0)681/302-2468     Geb. 36.1, Zi. 4.17, PF 151150
                               D-66041  Saarbrücken
http://www.rocksport.de        http://www.rocksport.de/first_ride.mp3




More information about the Python-list mailing list