944,089 Members | Top Members by Rank

Ad:
Oct 17th, 2006
0

Reading Binary Files in VB

Expand Post »
I have to write the following program and have no idea where to start. This is a binary file/image

1) Open an old file
2) Read until byte 56
3) Read the next 8 bytes to obtain the entire 9 digit cart frame
4) Keep reading byes until the HEX combination 49 49 2a 00 (HEX) is found.
5) When 49 49 2a 00 is found start writing an output file containing the bytes starting with 49 49 2a 00 For the output file use the cart-frame as the name of the file PLUS a number at the end to denote page number.
6) Keep reading and writing bytes to the output file UNTIL you reach end of file or you reach another 49 49 2a 00
7) If you hit another 49 49 2a 00 you will want to close the prior output file and open another output file. Increase the page number in the file name to denote the next page.
8) Repeat to process each old record

Does anyone have any suggestions on how to write this?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mooresh is offline Offline
3 posts
since Oct 2006
Oct 18th, 2006
0

Re: Reading Binary Files in VB

VB supports reading and writing to files using 'Open', 'Put' and 'Get' calls. Try this.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. dim v as integer
  2. dim offset as long
  3.  
  4. Open PFileName For Binary Access Read As #10
  5.  
  6. offset = 57
  7. get #10, offset, v

Note that offset is in bytes. You can write other logic as necessary. If you need more help, you may ask me.
Last edited by Comatose; Oct 23rd, 2006 at 10:38 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
udaywali is offline Offline
14 posts
since Oct 2006
Oct 22nd, 2006
1

Re: Reading Binary Files in VB

Here is a more complete and flexible example:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. ...
  2. OutputFileIndex = 0
  3. PageNum = 0
  4. ThisPage = ""
  5. InputFileIndex = FreeFile
  6. Open InputFileName For Binary Access Read As InputFileIndex
  7. CartNum = String(Chr(0), 8) ' This will make sure we grab 8 bytes.
  8. Get InputFileIndex, 56, CartNum ' Get Len(CartNum) bytes from the file
  9. ' pointed to by InputFileIndex and
  10. ' starting at byte 56, and store them
  11. ' in CartNum.
  12. LastFour = String(Chr(0), 4)
  13. Do Until EOF(InputFileIndex)
  14. JustOne = Chr(0)
  15. Get InputFileIndex, , JustOne ' Get Len(JustOne) bytes.
  16. LastFour = Mid(LastFour, 2) & JustOne ' Keep track of the last four bytes
  17. ' we've acquired.
  18. If LastFour = Chr(&H49) & Chr(&H49) & Chr(&H2A) & Chr(&H00) Then
  19. If PageNum > 0 Then ' Don't write the extra info before
  20. ' the first page.
  21. OutputFileIndex = FreeFile
  22. Open "Cart " & CartNum & ".Page " & PageNum & "." & InputFileName For Binary Access Write As OutputFileIndex
  23. Put OutputFileIndex, , Mid(ThisPage, 1, Len(ThisPage) - 3)
  24. ' The last three bytes will be
  25. ' HEX 49 49 2A, which we don't want:
  26. ' write all but the last three bytes.
  27. Close OutputFileIndex
  28. ThisPage = LastFour ' Start a new page.
  29. End If
  30. PageNum = PageNum + 1
  31. Else
  32. ThisPage = ThisPage & JustOne ' Add the byte to the current page.
  33. End If
  34. Loop
  35. Close ' This will close *everything*, just
  36. ' in case.
  37. ...

You will undoubtedly have to tweak and otherwise modify this code to make it work properly with your own, but it gives the basic gist of what you're after. Hope this helps!

- Sendoshin
Reputation Points: 16
Solved Threads: 1
Light Poster
sendoshin is offline Offline
39 posts
since Sep 2006
Oct 23rd, 2006
0

Re: Reading Binary Files in VB

Thanks for your help! I really appreciate this. I'll let you knwo if I get it to work.

Thanks Again
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mooresh is offline Offline
3 posts
since Oct 2006
Oct 25th, 2006
0

Re: Reading Binary Files in VB

Ho do I grab 8 bytes as the file name? How do I check for four bytes in a row?

Private Sub Command1_Click()
Dim InputFileIndex As Integer
Dim OutputFileIndex As Integer
Dim Pagenum As Integer
Dim ThisPage As String
Dim strPath As String
Dim LastFour As String
Dim bytData As Byte
Dim CartNum As String

InputFileIndex = FreeFile

Dim InputFileName As String
Dim JustOne As String

InputFileName = "C:\Eastman Images\05070TYR1022478DS"
Open InputFileName For Binary Access Read As InputFileIndex

CartNum = String(0, 8) '''RETURNS NOTHING
Get InputFileIndex, 56, CartNum

'LastFour = String(Chr(0), 4) ''DOES NOT WORK
Do While Not EOF(InputFileIndex)
JustOne = Chr(0)
Get InputFileIndex, , JustOne
LastFour = Mid(LastFour, 2) & JustOne

If LastFour = Chr(&H49) & Chr(&H49) & Chr(&H2A) & Chr(&H0) Then
If Pagenum > 0 Then
OutputFileIndex = FreeFile
Open "cart " & CartNum & ".page " & Pagenum & "." & InputFileName For Binary Access Write As OutputFileIndex
Put OutputFileIndex, , Mid(ThisPage, 1, Len(ThisPage) - 3)

Close OutputFileIndex
ThisPage = LastFour
End If
Pagenum = Pagenum + 1
Else
ThisPage = ThisPage & JustOne
End If

Loop
Close
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mooresh is offline Offline
3 posts
since Oct 2006
Oct 11th, 2008
0

Re: Reading Binary Files in VB

Click to Expand / Collapse  Quote originally posted by mooresh ...
How do I grab 8 bytes as the file name?
...
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. CartNum = String(0, 8) '''RETURNS NOTHING
  2. Get InputFileIndex, 56, CartNum
First off, you're absolutely right. String(0, 8) DOES return nothing. Or rather, it returns nothing VISIBLE. In reality, CartNum becomes an 8-byte string filled with zeroes. If you were to look at the string in a hex editor, it would show "0000000000000000" - eight bytes of zeroes. The thing is, Chr(0) (the character whose ASCII code is 0) is completely invisible - it shows up as nothing. So what you end up with is 8 bytes of nothing. You could just as easily use 32 (space) or 65 (A) instead of 0; I personally prefer to use 0. However, the fact that it is 8 bytes long is what is important for the next step:

"Get", when used on a file opened For Binary, will return a number of bytes equal to the length of the string used as its third argument. What this means is that if CartNum is 8 bytes when Get is called, Get will put 8 bytes from the file into CartNum. If CartNum was 0 bytes long (the default Null value which all VB strings are initialized to), Get would return 0 bytes instead. So in the end, getting 8 bytes from a file requires setting up an 8-byte-long string (via String(0, 8) in this case), and then calling Get on a file opened For Binary. (A three step process!)

Click to Expand / Collapse  Quote originally posted by mooresh ...
How do I check for four bytes in a row?
...
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. 'LastFour = String(Chr(0), 4) ''DOES NOT WORK
  2. Do While Not EOF(InputFileIndex)
  3. JustOne = Chr(0)
  4. Get InputFileIndex, , JustOne
  5. LastFour = Mid(LastFour, 2) & JustOne
  6.  
  7. If LastFour = Chr(&H49) & Chr(&H49) & Chr(&H2A) & Chr(&H0) Then
  8. ...
Here you caught a mistake I made. I was thinking that String() expected a string value as its first argument, when in reality it wants a number! The line should read LastFour = String(0, 4) . At any rate, things continue normally from there. We start a loop that will continue until we run out of data. Then we (re)set JustOne to an empty string one byte long, so that the Get line below it will pull out exactly one byte. Once Get does its thing (we left out the second argument to tell Get to continue where it left off), JustOne has the next byte in the file. The next step is to drop the first byte of LastFour (making it three bytes long) while simultaneously adding JustOne to the end (bringing it back up to four bytes). If we don't set LastFour to a 4-byte-long string (using the corrected version of the LastFour = ... line), the program will still try to drop the first byte from LastFour each time through, but since there isn't anything there for it to drop, the ultimate result will be that LastFour is only a single byte long - which is misleading and distinctly unhelpful.

As far as actually checking four bytes in a row, if LastFour is initialized correctly, the next line will check four bytes from the file for you. The &H inside each Chr() tells VB to read the numbers as hex values instead of decimal, so the If...Then will check for the four-byte sequence that separates each page. After that, things continue onward, splitting the old file into the new format.

Hopefully that helps, although I sense it's a bit late. ^_^;;

- Sen
Reputation Points: 16
Solved Threads: 1
Light Poster
sendoshin is offline Offline
39 posts
since Sep 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: about inputBox
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: How to set Database Applicaion path to crystal Reports





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC