+ Reply to Thread
Results 1 to 4 of 4

Reading random binary file with header

  1. #1
    Registered User
    Join Date
    09-30-2016
    Location
    Munich, Germany
    MS-Off Ver
    2013
    Posts
    17

    Reading random binary file with header

    Hello all,

    I am trying to read from a binary file which has the following structure:
    - Part 1: header, around 433 Bytes
    - Part 2: Data-structures, 40 Bytes long

    What I have found up to now is to read/jump in chunks of record-lengths:
    Dim MyDataset As my_type
    Open "whatever" For Random As #1 Len = Len(MyDataset)

    This works well if the whole binary file is filled with all equal data, however not if I have two separate portions of different lengths.
    Is there an (elegant) way to come around that? I am mainly interested in reading the second portion with the data-structures.

    Cheers,

    Moadll

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Reading random binary file with header

    Hi Moadll,

    Try the following code (includes a routine to output a sample binary file per your specs) that reads/writes one byte at a time. When reading past the End of the File, the file will close.

    Please Login or Register  to view this content.
    It is a best practice to declare all variables. If you misspell a variable in your code, VBA will silently assume it is a Variant variable and go on executing with no clue to you that you have a bug. Go to the VBA development window, click Tools, Options, and check "Require Variable Declaration." This will insert the following line at the top of all new modules:
    Please Login or Register  to view this content.
    This option requires all variables to be declared and will give a compiler error for undeclared variables.

    I hope this helps.

    Lewis
    Last edited by LJMetzger; 12-22-2016 at 04:48 PM.

  3. #3
    Registered User
    Join Date
    09-30-2016
    Location
    Munich, Germany
    MS-Off Ver
    2013
    Posts
    17

    Re: Reading random binary file with header

    Hello Lewis,

    thanks for the pointer. I went for a slightly different version of that that works with less loops (I have a very large data-set to go through where time starts to matter):
    Please Login or Register  to view this content.
    With that I get the intial 433 Bytes read in one go and then read the chunks of data-sets also in one go per data-set.
    What I cannot do is to directly jump to a certain data-set. That would be nice, however not essential here.
    The other nice thing with that is that I already get the binary data brought onto the variables. The TIME_DATE is a 4 Byte value where I was fearing I'd have to go for a loop of reading a byte, multiplying it with 256, 256^2 or 256^3 to get the variable properly populated.

    Sometimes automatisms aren't that bad... 8o)

    Cheers,

    Moadll
    Last edited by Moadll; 12-23-2016 at 04:41 AM.

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Reading random binary file with header

    Nice job. I put my code together quickly with something I knew that worked, since I haven't had a need to access binary files in many years.. Your use of the data structure makes accessing the data records simple.

    Lewis

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Need help converting Hex to Long / Reading file contents at binary level
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-04-2015, 07:51 PM
  2. Importing a Binary File, displaying as Hex, and saving the file as a Binary file again
    By bluenose616 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-26-2014, 04:46 PM
  3. [SOLVED] random binary output
    By schill2 in forum Excel General
    Replies: 6
    Last Post: 10-19-2013, 03:38 AM
  4. Reading a binary file
    By Andrew-R in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-13-2012, 10:16 AM
  5. Reading binary file
    By Maxi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-14-2006, 07:20 PM
  6. [SOLVED] Solver returns non binary answer in binary constrained cells
    By Tushar Mehta in forum Excel Formulas & Functions
    Replies: 18
    Last Post: 09-06-2005, 08:05 AM
  7. Solver returns non binary answer in binary constrained cells
    By Navy Student in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1