Read long int from binary file

E

Enrico Morelli

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?

Thanks a lot
Enrico
 
P

Paul Rubin

Enrico Morelli said:
I cannot know the format (little or big endian) and I have to perform the
properly 4 byte reverse order swapping.

If you don't know the endianness, you can't tell whether to swap or not.
 
J

John Roth

Enrico Morelli said:
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
 
P

Peter Hansen

Enrico said:
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
 
P

Phil Stracchino

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.

Is it feasible for you to have a known value placed at the start of the
file as a "magic" number that you can use for endianness detection?


--
.********* Fight Back! It may not be just YOUR life at risk. *********.
: phil stracchino : unix ronin : renaissance man : mystic zen biker geek :
: (e-mail address removed) : (e-mail address removed) : (e-mail address removed) :
: 2000 CBR929RR, 1991 VFR750F3 (foully murdered), 1986 VF500F (sold) :
: Linux Now! ...Because friends don't let friends use Microsoft. :
 
E

Enrico Morelli

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
 
A

Alex Martelli

Enrico Morelli wrote:
...
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.


import array

x1 = array('l')
f1 = file('file_ok.dat', 'rb')
x1.fromfile(f1)
f1.close()

x2 = array('l')
f2 = file('file_toswap.dat', 'rb')
x2.fromfile(f2)
x2.byteswap()
f2.close()


Alex
 
E

Enrico Morelli

Enrico Morelli wrote:
...


import array

x1 = array('l')
f1 = file('file_ok.dat', 'rb')
x1.fromfile(f1)
f1.close()

x2 = array('l')
f2 = file('file_toswap.dat', 'rb')
x2.fromfile(f2)
x2.byteswap()
f2.close()


Alex

Thanks Alex!!!

A question, the fromfile syntax wants the n items to read.
x1.fromfile(f1,1) read exactly one 32 bit long integer?


Enrico

PS. Sei sempre 'r mejo!!
 
A

Alex Martelli

Enrico Morelli wrote:
...
...
Thanks Alex!!!
Prego!-)


A question, the fromfile syntax wants the n items to read.

Yes, sorry.
x1.fromfile(f1,1) read exactly one 32 bit long integer?

Yes. x1.fromfile(f1,999) reads UP TO 999 long integers -- will
probably raise EOFError by finding less than 999 there, so use:

try: x1.fromfile(f1, 999)
except EOFError: pass

Also remember fromfile APPENDS to whatever was already in the
array x, so make sure x is empty before x.fromfile if needed.

Enrico

PS. Sei sempre 'r mejo!!

Troppo'bbono dotto`...;-)


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top