Results 1 to 4 of 4

Thread: Reading/Saving binary data

  1. #1
    Senior Member
    Join Date
    Apr 2011
    Posts
    190
    Rep Power
    14

    Reading/Saving binary data

    Below is my code (somehow the code tags are gone - so sorry-cannot post using code tags). I open the source file(#2) as a binary file and the destination file (#1) for random access. Next I do some large loops reading 4 byte single values from the source file and then save them in the destination file - for every oberservation (iix) - I have 1100 values each 4 bytes to read. As you can see the 1100 values that I read for each of the iix loop is consequtive - so is there a way I read the 1100 byte single values in a single operation and also store them in a single operation. The current loop is quite slow.

    Code:
    For iix = 0 To 20000
            AAXdouble = ThisWorkbook.Worksheets("TempSample").Cells(iix, ColSpectraPosition)
            For iiix = 0 To 9999
                Get #2, Adouble, Asingle
                Put #1, AXdouble, Asingle
                Adouble = Adouble + 4
                AXdouble = AXdouble + 4
            Next iiix
     Next iix
    Last edited by Excel Fox; 02-25-2012 at 11:47 PM. Reason: Code Tags
    xl2007 - Windows 7
    xl hates the 255 number

  2. #2
    Senior Member
    Join Date
    Apr 2011
    Posts
    190
    Rep Power
    14
    ok - I figured out how to read a chunck of data - It is as simple as using a string buffered with in this case 4400 spaces

    Code:
    Absorbancies = Space$(4400) ' Set our buffer (string) to that length.
    
    
            Get #2, Adouble, Absorbancies  ' Grab a chunk of data from the file.
            Put #1, AXdouble, Absorbancies
    Last edited by Excel Fox; 02-26-2012 at 03:01 PM. Reason: Code Tags
    xl2007 - Windows 7
    xl hates the 255 number

  3. #3
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Thanks for sharing the solution Rasm.... can you post the whole code for posterity
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  4. #4
    Senior Member
    Join Date
    Apr 2011
    Posts
    190
    Rep Power
    14
    Read/Write chunks of binary data - I have simplified the code here - so this is simply an example - you have to set your own paramters

    The code listed here – reads chunks of data from one binary file and saves it to another binary file. In this example I have 20K observations (samples) – for each observation I have 1100 single precision values. This code reads all the 1100 single values for each observation in one operation rather than a loop reading 1100 single values one by one. That way the code is much faster.

    Code:
    Public Sub ReadWriteBinary(ByVal PathNameSourceFile As String, ByVal PathNameDestinationFile As String)
       
        Dim Buffer As String
        Dim NumberOfValues As Long, Xlong As Long
        
        ' The number of blocks of data - In this case I have 20K records where I want to copy data
        NumObservations = 20000
        ' This example has 1100 values each a 4 byte number of the data type single precision.
        NumberOfValues = 1100
        'In this example I want to read a series of 4 byte Single data types (Single) for each observation
        Xlong = 4 * NumberOfValues
        ' Set the buffer for each chunk of data to read
        Buffer = Space$(Xlong)
        
        Open PathNameSourceFile For Binary As #2
        Open PathNameDestinationFile For Binary As #1
        For iix = 0 To NumObservations - 1
            ' This is the location in Bytes where the source data reside
            LocalOfDataSource = iix * 5000 + 512
            'This is the location in Bytes where I want to save the data
            LocalOfDestinationData = iix * 8000 + 1024
            ' reads a chunk of data from the source file.
            Get #2, LocalOfDataSource, Buffer
            'Saves the data just read to the destination file
            Put #1, LocalOfDestinationData, Buffer
        Next iix
        Close #1
        Close #2
    End Sub
    xl2007 - Windows 7
    xl hates the 255 number

Similar Threads

  1. Saving and Running Macro For Multiple Files / Users
    By Charles_ in forum Excel Help
    Replies: 1
    Last Post: 01-07-2013, 09:10 AM
  2. Message Box Before Saving Document
    By Lucero in forum Excel Help
    Replies: 2
    Last Post: 04-15-2012, 07:09 AM
  3. Reading Access tables using INNER JOIN
    By Rasm in forum Excel Help
    Replies: 2
    Last Post: 02-04-2012, 09:17 PM
  4. Saving Embedded Picture From Excel Workbook Sheet To Folder Hard Drive
    By littleiitin in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 10-31-2011, 02:31 PM

Posting Permissions

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