Read long int from binary file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Enrico Morelli

    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

    #2
    Re: Read long int from binary file

    Enrico Morelli <enrico_morelli @yahoo.com> writes:[color=blue]
    > I cannot know the format (little or big endian) and I have to perform the
    > properly 4 byte reverse order swapping.[/color]

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

    Comment

    • John Roth

      #3
      Re: Read long int from binary file


      "Enrico Morelli" <enrico_morelli @yahoo.com> wrote in message
      news:pan.2003.1 0.14.15.37.50.6 02541@yahoo.com ...[color=blue]
      > 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?[/color]

      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[color=blue]
      >
      > Thanks a lot
      > Enrico[/color]


      Comment

      • Peter Hansen

        #4
        Re: Read long int from binary file

        Enrico Morelli wrote:[color=blue]
        >
        > 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.[/color]

        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

        Comment

        • Phil Stracchino

          #5
          Re: Read long int from binary file

          On Tue, Oct 14, 2003 at 12:19:25PM -0400, John Roth wrote:[color=blue]
          > "Enrico Morelli" <enrico_morelli @yahoo.com> wrote in message
          > news:pan.2003.1 0.14.15.37.50.6 02541@yahoo.com ...[color=green]
          > > 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.[/color]
          >
          > 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.[/color]

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

          Comment

          • Enrico Morelli

            #6
            Re: Read long int from binary file

            On Tue, 14 Oct 2003 12:28:43 -0400, Peter Hansen wrote:
            [color=blue]
            > Enrico Morelli wrote:[color=green]
            >>
            >> 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.[/color]
            >
            > 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[/color]
            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

            Comment

            • Alex Martelli

              #7
              Re: Read long int from binary file

              Enrico Morelli wrote:
              ...[color=blue]
              > 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.[/color]


              import array

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

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


              Alex

              Comment

              • Enrico Morelli

                #8
                Re: Read long int from binary file

                On Wed, 15 Oct 2003 08:06:56 +0000, Alex Martelli wrote:
                [color=blue]
                > Enrico Morelli wrote:
                > ...[color=green]
                >> 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.[/color]
                >
                >
                > import array
                >
                > x1 = array('l')
                > f1 = file('file_ok.d at', 'rb')
                > x1.fromfile(f1)
                > f1.close()
                >
                > x2 = array('l')
                > f2 = file('file_tosw ap.dat', 'rb')
                > x2.fromfile(f2)
                > x2.byteswap()
                > f2.close()
                >
                >
                > Alex[/color]

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

                Comment

                Working...