Reading a multi-byte number
Batista, Facundo
FBatista at uniFON.com.ar
Mon May 19 14:07:53 EDT 2003
I must read a 8bit number from a file. This is easy:
caract = myfile.read(1)
number = ord(caract)
But when I must read a 16bit number it's a little more complicated:
(caract1, caract2) = myfile.read(2)
number = ord(caract1) * 255 + ord(caract2)
When I must read a (8 * n)bit number, I need a for (this code it's not
tested):
def strInt(cadena):
exponentes = range(len(cadena)-1, -1, -1)
valor = 0
for ind in range(len(cadena)):
car = cadena[ind]
exp = exponentes[ind]
valor += ord(car) * (255 ^ exp)
return valor
There's a builtin way to do this?
Thank you!
. Facundo
More information about the Python-list
mailing list