Reading a multi-byte number

Batista, Facundo FBatista at uniFON.com.ar
Mon May 19 16:33:01 EDT 2003


#- >                 valor += ord(car) * (255 ^ exp)
#- 
#- (Btw, shouldn't that be "255**exp"?)

Yes, my mistake.


#-     def bytes2int(string):
#-         i = 0
#-         for ch in string:
#-             i = 255*i + ord(ch)
#-         return i

I like it!! One detail: it's 256, not 255.

I've written this one:

>>> def strToInt(value):
...     cad = ""
...     for carac in value:
...             cad += hex(ord(carac))[2:]
...     return eval("0x" + cad)

But I think yours is faster, :)

Thanks Steven!

.	Facundo





More information about the Python-list mailing list