Solved

Binary file access in VB6

Posted on 2008-10-17
6
747 Views
Last Modified: 2011-10-19
I usually manage to find solutions to stuff like this on my own, but i'm afraid this time i've come away empty handed. Hopefully someone can help clear this up for me.

I am trying to read a binary file in visual basic 6, but the file has a custom type.
The values are not stored as regular bytes, but as bit values of varying lengths.
For example one value i am trying to get is stored in 20bits, another in just 5.
I'm pretty good at VB, and when it comes to reading a normal file with 8, 16 or 32 bit values i can handle that fine.

The file type in question is actually a FLAC file (a compressed WAVE audio file). The full specification is here: http://flac.sourceforge.net/format.html#stream

I'm not in need of parsing the whole file. But i need to get the "Sample rate in Hz" value stored in 20 bits and the "(bits per sample)-1" stored in 5 bits.

I have attached a sample FLAC file, where the bits per sample is 16 and sample rate is 44100.
(edit: the file in the zip is called sample_flac.mdb, rename from .mdb to .flac)

Anyone think they can help?
0
Comment
Question by:twiglatte
  • 3
  • 2
6 Comments
 
LVL 31

Expert Comment

by:Frosty555
Comment Utility
Binary file access has always been a pain in the butt when it comes to VB6. FileSystemObject only works for text files.

Here is an example of how to read binary files in VB6:
http://www.computing.net/answers/programming/vb6-readwrite-binary-file/1965.html

Basically, use VB's native "Open" keyword to open the file, and add "For Binary" on the end.
Get bytes of the file using the "Get" keyword. Get will fill whatever data you pass it with as many bytes as necessary, read from the file.

It's impossible to find good reference material for VB6 anymore by just googling it, so I'll give you the links to the MSDN:

VB6 Language Reference:
http://msdn.microsoft.com/en-us/library/aa338033(VS.60).aspx
Open statement
http://msdn.microsoft.com/en-us/library/aa266177(VS.60).aspx
Get statement
http://msdn.microsoft.com/en-us/library/aa243376(VS.60).aspx

Once you've read in the bytes you need, if you want to do bit-level calculations on it, use boolean logic. A nice page that discusses boolean logic, even with some VB6 examples:
http://www.ambrsoft.com/binary.html

You'll just have to do some clever math on the data to shift the bits or trim off the ones you don't need.
0
 

Author Comment

by:twiglatte
Comment Utility
Like i said, i'm already familiar with VB's native file operations. That isn't the problem.

The problem is the "clever math" that you refer to at the end of your post that i would like help with.
For example reading 5bits starting at the Nth bit of a file.
0
 
LVL 31

Accepted Solution

by:
Frosty555 earned 500 total points
Comment Utility
To add X number of bits to the right of a number, multiply by 2^X

e.g.  a = 1001 binary
        b = a * (2^4) = 10010000 binary

To trim X number of bits off the right of a number, divide by 2^X and get the INT() of that.

e.g. a = 11001010 (binary)
       b = Int(a / (2^4)) = 1100 binary

To concatenate an N bit number to the end of an M bit number, add N bits to the right of number M, then add N to that number.

e.g. a = 100000 (binary)
       b = 110 (binary)
       c = (a * 2^3) +  b = 100000110

To trim all but X bits off the LEFT of a number, simply AND the number with (2^X)-1.

e.g. a = 110101100(binary)
       b = a AND ((2^3) - 1) = 000000100(binary) = 100(binary)


Between those three formulas you should be able to slice up your data and get what you're looking for.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
You have forgotten to attach the file.
0
 

Author Comment

by:twiglatte
Comment Utility
No i didn't, it just never showed up. I already had to change the extension and then zip the file, but when it finally accepted the upload it never showed up on the question. I figured it would just take some time.

I cant edit the question any more, so here is the file: http://enpts.com/sample_flac.zip
0
 

Author Closing Comment

by:twiglatte
Comment Utility
Thanks, it took me a while to figure out how to implement this, but this is what i needed to know
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This is about my first experience with programming Arduino.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now