[Tutor] Handling binary file
John Purser
johnp at milwaukielumber.com
Thu Aug 25 18:44:47 CEST 2005
Julie,
I've had to work with some binary files with 3 byte data types. They were
generated by an application coded in Business Basic. Like you I grabbed it
as bytes and converted. For clarity's sake I used powers of 2 instead of
hard coding the numbers. 2^8 makes it pretty obvious why you're multiplying
by 256 IMHO. Look out for negative numbers though. May not be applicable
to "number of seconds" but as a general rule watch for it. Incidentally
when I had to read the same data with VBA I used the same technique to
reverse the byte order from Unix to Windows. Worked just fine.
John Purser
_____
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On Behalf
Of Julie Lai
Sent: Wednesday, August 24, 2005 18:20
To: tutor at python.org
Subject: [Tutor] Handling binary file
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? Thanks,
Julie.
_______________________________________
想即時收到新 email 通知?
下載 Yahoo! Messenger http://messenger.yahoo.com.hk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050825/ac0173f5/attachment.htm
More information about the Tutor
mailing list