Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Read long int from binary file

Reply
Thread Tools

Read long int from binary file

 
 
Enrico Morelli
Guest
Posts: n/a
 
      10-14-2003
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
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      10-14-2003
Enrico Morelli <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
John Roth
Guest
Posts: n/a
 
      10-14-2003

"Enrico Morelli" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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



 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      10-14-2003
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
 
Reply With Quote
 
Phil Stracchino
Guest
Posts: n/a
 
      10-14-2003
On Tue, Oct 14, 2003 at 12:19:25PM -0400, John Roth wrote:
> "Enrico Morelli" <(E-Mail Removed)> wrote in message
> news(E-Mail Removed)...
> > 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 :
: http://www.velocityreviews.com/forums/(E-Mail Removed) : (E-Mail Removed) : (E-Mail Removed) :
: 2000 CBR929RR, 1991 VFR750F3 (foully murdered), 1986 VF500F (sold) :
: Linux Now! ...Because friends don't let friends use Microsoft. :

 
Reply With Quote
 
Enrico Morelli
Guest
Posts: n/a
 
      10-15-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
 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      10-15-2003
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

 
Reply With Quote
 
Enrico Morelli
Guest
Posts: n/a
 
      10-15-2003
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!!
 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      10-15-2003
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Promoting unsigned long int to long int pereges C Programming 112 07-28-2008 05:00 AM
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
unsigned long long int to long double Daniel Rudy C Programming 5 09-20-2005 02:37 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments