Solved

Binary file access in VB6

Posted on 2008-10-17
6
760 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

726 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