Read long int from binary file

Enrico Morelli enrico_morelli at yahoo.com
Wed Oct 15 03:53:31 EDT 2003


On Tue, 14 Oct 2003 12:28:43 -0400, Peter Hansen wrote:

> Enrico Morelli wrote:
>> 
>> I have to write a program which reads from a binary file, a serious of
>> 32 bit long integer data and stores its in an array.
>> I cannot know the format (little or big endian) and I have to perform the
>> properly 4 byte reverse order swapping.
> 
> Do you really mean that you must support both formats?  In other words,
> that you can't *hardcode* the choice of format, but must support either
> one?  (Presumably based on some command-line option, or information that
> is contained elsewhere but which is available at program runtime.)
> 
> I'm guessing the confusion results from uncertain English usage... One
> would normally say "I do not know the format (in advance)" rather than
> "I cannot know the format".  If you really mean you *cannot* know, then
> why would you expect that the computer "could" know something you cannot?
> 
> -Peter
You are ready. My english is very bad :-(
I do know the format (in advance).
I have some binary files coming from SGI boxes and other from Linux boxes.
These files contains 32 bit long integer data that I need to read and
display in some graphic format.
In some cases I have to reverse the byte order in other not.

I'm unable to read these files and put the data in some array.
I tried to use f.read(4), but I have 4 numbers not one.

Thanks at all for your help.
Enrico




More information about the Python-list mailing list