0
votes

I think I understand this correctly, but I just want to double check to be sure. Suppose I write a binary file using VBA where the first X number of bytes represents some field, the next x number another field, and so on. Now suppose I read that binary file back into VBA later using a byte array. Is it reasonable to assume that the first x elements in the byte array directly correlate to the first x bytes in the file?

I should have made this clear from the get-go, the format and header of the file isn't all that important, I'm just trying to get more into the nitty-gritty of reading and writing binary files and using byte arrays with vba. I'm getting there, and I appreciate everyone's input.

1
The answer to your question is "Yes". If you're asking how you can do that, I can provide an example. - Blackhawk
I've actually been messing around with it for a few minutes. Sometimes doing by learning seems best. I appreciate it though. That answer gets me on the right track. I'm just using the vba primitives for now. - MattB
If you are attempting to parse an established file type such as .jpg or .doc etc, using User Defined Types is often an easy way to go. The VBA Put and Get functions allow you to read and write those structures directly from binary, which is incredibly handy. Here is an example of using UDT to read a BMP file. - Blackhawk
OH! One caveat - when you write multi-byte types to a file, they are stored in Little Endian. For example, if you have a Long that holds the value 12345 ([00 00 30 39] in hexadecimal) it will be stored to the file in the reverse byte order of what you might think: [39 30 00 00]. Each pair of hexadecimal "digits" represents a byte. If you read the Long out of the file using Get, VBA automatically understands that the most significant byte is the right-most, but if you read it as a byte array, you would have to know that. - Blackhawk
Actually, I abandoned this approach because I found a database. Sort of. Our exchange server. The information pulled in by this macro is parsed from emails sent to a particular email address. So, I decided to start experimenting with treating outlook and the exchange server as a database. Worked out much better than expected. Thanks for the help nonetheless! At least I finally got this one figured out. - MattB

1 Answers

0
votes

When writing binary files, write headers to check if the given bytes are really matching your format. Take a look into a wave file with a hex editor (i use HxD) and you will see something like this:

RIFFŽ...WAVEfmt ........D¬...±......dataà€....

  1. RIFF is the header of the container (resource interchange format)
  2. followed by some bytes for meta information
  3. WAVE is the header of the actual wave
  4. then some data follows you might want to interprete

Here are two example binary read/write methods (converted to VB.NET from C#)

Public Shared Sub Write(This As YOUR_TYPE, stream As BinaryWriter)
stream.Write(FILE_IDENTIFIER)
stream.Write(FILE_VERSION)
While True
    stream.Write(/*...*/)
End While
End Sub

Public Shared Function read(stream As BinaryReader) As YOUR_TYPE
If Not Enumerable.SequenceEqual(FILE_IDENTIFIER, stream.ReadBytes(FILE_IDENTIFIER.Length)) Then
    Throw New FormatException("header mismatch")
End If
If stream.ReadByte() <> FILE_VERSION Then
    Throw New NotSupportedException("version mismatch")
End If

Dim result As New YOUR_TYPE()
While True
    stream.Read(/*...*/)
    End While
Return result
End Function