Read long int from binary file

Discussion in 'Python' started by Enrico Morelli, Oct 14, 2003.

  1. 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
    Enrico Morelli, Oct 14, 2003
    #1
    1. Advertising

  2. Enrico Morelli

    Paul Rubin Guest

    Enrico Morelli <> 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.
    Paul Rubin, Oct 14, 2003
    #2
    1. Advertising

  3. Enrico Morelli

    John Roth Guest

    "Enrico Morelli" <> wrote in message
    news:p...
    > 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
    John Roth, Oct 14, 2003
    #3
  4. Enrico Morelli

    Peter Hansen Guest

    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
    Peter Hansen, Oct 14, 2003
    #4
  5. On Tue, Oct 14, 2003 at 12:19:25PM -0400, John Roth wrote:
    > "Enrico Morelli" <> wrote in message
    > news:p...
    > > 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 :
    : : : :
    : 2000 CBR929RR, 1991 VFR750F3 (foully murdered), 1986 VF500F (sold) :
    : Linux Now! ...Because friends don't let friends use Microsoft. :
    Phil Stracchino, Oct 14, 2003
    #5
  6. 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
    Enrico Morelli, Oct 15, 2003
    #6
  7. 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
    Alex Martelli, Oct 15, 2003
    #7
  8. 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!!
    Enrico Morelli, Oct 15, 2003
    #8
  9. 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
    Alex Martelli, Oct 15, 2003
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,195
    Martien Verbruggen
    Jun 27, 2003
  2. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,173
    Peter Shaggy Haywood
    Sep 20, 2005
  3. pereges

    Promoting unsigned long int to long int

    pereges, Jun 30, 2008, in forum: C Programming
    Replies:
    112
    Views:
    2,024
    David Thompson
    Jul 28, 2008
  4. veryhotsausage
    Replies:
    1
    Views:
    1,771
    veryhotsausage
    Jul 4, 2008
  5. Oliver Graeser
    Replies:
    10
    Views:
    570
    Oliver Graeser
    Sep 26, 2008
Loading...

Share This Page