Read long int from binary file

John Roth newsgroups at jhrothjr.com
Tue Oct 14 12:19:25 EDT 2003


"Enrico Morelli" <enrico_morelli at yahoo.com> wrote in message
news:pan.2003.10.14.15.37.50.602541 at yahoo.com...
> Dear all,
>
> 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.
>
> Someone can help me?
> Where I can found some infos?

As a general rule, the problem is unsolvable. However, there are
two practical special cases. One is that you may be able to determine
based on the source of the file; different computers and different
protocols have specific requirments.

The other is that you can usually tell by inspecting a number of
values, based on the observation that small numbers are a lot
more prevalent than large ones. Other criteria may be necessary
in your application, but it's usually possible to run a sample of a
hundred or so through a discrimination function and get a reliable
opinion.

John Roth
>
> Thanks a lot
> Enrico






More information about the Python-list mailing list