Solved

Binary file access in VB6

Posted on 2008-10-17
6
755 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 31

Expert Comment

by:Frosty555
ID: 22747261
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
ID: 22747287
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
ID: 22747294
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 22747450
You have forgotten to attach the file.
0
 

Author Comment

by:twiglatte
ID: 22749952
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
ID: 31507367
Thanks, it took me a while to figure out how to implement this, but this is what i needed to know
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

749 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