[Tutor] Handling binary file
Alan Gauld
alan.gauld at btinternet.com
Thu Aug 25 10:04:48 CEST 2005
> I have opened a file in binary mode.
> The 9th, 10th and 11th bytes contain the time in seconds.
> In order to get this value in decimal I did the following:
>
> timeinsec = bytes[9] * 65536 + bytes[10] * 256 + bytes{11]
>
> Would someone please advise if there is a better way to do
> this?
You could use:
timeinsec = bytes[9:12]
which gets you the three bytes as a sequence.
You should be able to use the struct module to convert that into
a number except that struct expects an integer to be 4 bytes so
you probably need to add a zero in front. It also depends on
issues like big-endian v little endian (ie right or left hand
alignment)
etc.
If your method works I wouldn't worry too much!
Alan G
More information about the Tutor
mailing list