Solved

Binary file access in VB6

Posted on 2008-10-17
6
751 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Path to Python 9 119
I need an assist with a programming logic math question. 5 64
clicking a shape in a frame array vb6 3 45
Delphi: barcode reading on android platform 1 30
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…
A short article about problems I had with the new location API and permissions in Marshmallow
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

777 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