Reading from a 64 bit data Binary File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vikuba
    New Member
    • Dec 2008
    • 4

    Reading from a 64 bit data Binary File

    HI I'm new to this forum

    I'm having a peculiar problem
    I'm trying to read a 64 bit data binary file

    I have 5 integer variables a b c d e

    the problem is I have to read the 64 bit data

    like 0-7 bits as integer value to a
    8-15 bits as int into b
    16-18 bits as int into c
    etc like that
    for eg if 0-7 bits are 0011010 then a should be a int value of 26

    my format of binary file looks like the following

    00000000 070E0000160A04C 0 031000000F01028 0 .......À....... €
    00000010 0A090000131401C 0 1E1700001A0601C 0 .......À....... À
    00000020 1A0B05001E1701C 0 18110100031603C 0 .......À....... À
    00000030 090C0000130001C 0 00120700140E03C 0 .......À....... À
    00000040 18150500010E038 0 0B160600160602C 0 .......€....... À
    00000050 0A0E0000130B01C 0 19010100050803C 0 .......À....... À
    00000060 1B0801000C0F03C 0 011607001D1303C 0 .......À....... À

    so in the first line I'm interested only in 070E0000160A04C 0 since this is the 64 bit data

    so for the binary file I set the hex line length as 8 and the view changes to
    lsb msb
    00000000 070E0000160A04C 0 .......À
    00000008 031000000F01028 0 .......€
    00000010 0A090000131401C 0 .......À
    00000018 1E1700001A0601C 0 .......À
    00000020 1A0B05001E1701C 0 .......À
    00000028 18110100031603C 0 .......À
    00000030 090C0000130001C 0 .......À

    Which format you think will be better

    I'm completely new to reading binary files or binary format itself

    Can you please let me know how to read specific bits to a specific variable

    A code sample would be of great help
    Thank you
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You can read specific bits out of a file, the minimum you can read is a byte. You need to read all the bytes of data you are interested in (8 if its 64 bits) and then use the bitwise operators to extract from those 8 bytes the bits you are interested in into your 5 integer variables.

    Comment

    • vikuba
      New Member
      • Dec 2008
      • 4

      #3
      thanx I think it got what you are saying

      but would like to know how to read the binary file byte wise

      a small sample code would be of great help
      thanx

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        C or C++?

        Comment

        • vikuba
          New Member
          • Dec 2008
          • 4

          #5
          if only i knew a way to do it in C

          I'm asking for your help regarding the code to do it

          I do not know a way of reading a binary file byte wise using C

          Please help me regarding the code

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Look up the functions

            fopen - opens the file
            fread - reads data from the file
            fclose - closes the file

            in your C text book or in the links given in the sticky post at the top of this forum.

            Comment

            • vikuba
              New Member
              • Dec 2008
              • 4

              #7
              Originally posted by Banfa
              C or C++?
              Why do you think I would post it in the c/c++ forum if I did not know that it can be done using C or C++

              o my god
              thanx for the great help mr.Banfa

              and realize that people post it in a forum after trying it first

              requesting a solution in a forum would be the last option for anyone

              thats it i'm outta here

              Comment

              • Tassos Souris
                New Member
                • Aug 2008
                • 152

                #8
                One thing that you should consider is that when using binary files you definitely hurt the portability of your program. That is because data read in from a binary stream shall compare equal to data that were written earlier to that stream under the SAME implementation. Otherwise you do not know what you get. Well, even in the same implementation you might get some tricky null padding there (correct me if i am wrong)...
                If you do that because you want to exercise binary formats then ok.
                If you do that for a program that needs to write in binary files that you do not care about moving them to another machine then you should consider the easiest way of a struct definition and a fwrite call (then retrieving them with fread).
                If you do that for a program that needs data to be shared within programs then learn XML :-)

                But because i suspect you're doing that to learn then these function mentioned by Banfa are all you need :-) :

                Comment

                Working...