Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Read long int from binary file (http://www.velocityreviews.com/forums/t323651-read-long-int-from-binary-file.html)

Enrico Morelli 10-14-2003 03:37 PM

Read long int from binary file
 
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

Paul Rubin 10-14-2003 03:46 PM

Re: Read long int from binary file
 
Enrico Morelli <enrico_morelli@yahoo.com> writes:
> 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.

John Roth 10-14-2003 04:19 PM

Re: Read long int from binary file
 

"Enrico Morelli" <enrico_morelli@yahoo.com> wrote in message
news:pan.2003.10.14.15.37.50.602541@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




Peter Hansen 10-14-2003 04:28 PM

Re: Read long int from binary file
 
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

Phil Stracchino 10-14-2003 04:28 PM

Re: Read long int from binary file
 
On Tue, Oct 14, 2003 at 12:19:25PM -0400, John Roth wrote:
> "Enrico Morelli" <enrico_morelli@yahoo.com> wrote in message
> news:pan.2003.10.14.15.37.50.602541@yahoo.com...
> > 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.

>
> 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 :
: alaric@caerllewys.net : alaric-ruthven@earthlink.net : phil@latt.net :
: 2000 CBR929RR, 1991 VFR750F3 (foully murdered), 1986 VF500F (sold) :
: Linux Now! ...Because friends don't let friends use Microsoft. :


Enrico Morelli 10-15-2003 07:53 AM

Re: Read long int from binary file
 
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

Alex Martelli 10-15-2003 08:06 AM

Re: Read long int from binary file
 
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


Enrico Morelli 10-15-2003 09:40 AM

Re: Read long int from binary file
 
On Wed, 15 Oct 2003 08:06:56 +0000, Alex Martelli wrote:

> 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


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!!

Alex Martelli 10-15-2003 10:03 AM

Re: Read long int from binary file
 
Enrico Morelli wrote:
...
>> x1 = array('l')
>> f1 = file('file_ok.dat', 'rb')
>> x1.fromfile(f1)

...
> 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



All times are GMT. The time now is 08:50 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.