| | |
Reading Binary Files in VB
![]() |
•
•
Join Date: Oct 2006
Posts: 3
Reputation:
Solved Threads: 0
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?
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?
•
•
Join Date: Oct 2006
Posts: 14
Reputation:
Solved Threads: 0
VB supports reading and writing to files using 'Open', 'Put' and 'Get' calls. Try this.
Note that offset is in bytes. You can write other logic as necessary. If you need more help, you may ask me.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
dim v as integer dim offset as long Open PFileName For Binary Access Read As #10 offset = 57 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.
•
•
Join Date: Sep 2006
Posts: 36
Reputation:
Solved Threads: 1
Here is a more complete and flexible example:
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
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
... OutputFileIndex = 0 PageNum = 0 ThisPage = "" InputFileIndex = FreeFile Open InputFileName For Binary Access Read As InputFileIndex CartNum = String(Chr(0), 8) ' This will make sure we grab 8 bytes. Get InputFileIndex, 56, CartNum ' Get Len(CartNum) bytes from the file ' pointed to by InputFileIndex and ' starting at byte 56, and store them ' in CartNum. LastFour = String(Chr(0), 4) Do Until EOF(InputFileIndex) JustOne = Chr(0) Get InputFileIndex, , JustOne ' Get Len(JustOne) bytes. LastFour = Mid(LastFour, 2) & JustOne ' Keep track of the last four bytes ' we've acquired. If LastFour = Chr(&H49) & Chr(&H49) & Chr(&H2A) & Chr(&H00) Then If PageNum > 0 Then ' Don't write the extra info before ' the first page. OutputFileIndex = FreeFile Open "Cart " & CartNum & ".Page " & PageNum & "." & InputFileName For Binary Access Write As OutputFileIndex Put OutputFileIndex, , Mid(ThisPage, 1, Len(ThisPage) - 3) ' The last three bytes will be ' HEX 49 49 2A, which we don't want: ' write all but the last three bytes. Close OutputFileIndex ThisPage = LastFour ' Start a new page. End If PageNum = PageNum + 1 Else ThisPage = ThisPage & JustOne ' Add the byte to the current page. End If Loop Close ' This will close *everything*, just ' in case. ...
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
•
•
Join Date: Oct 2006
Posts: 3
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Sep 2006
Posts: 36
Reputation:
Solved Threads: 1
•
•
•
•
How do I grab 8 bytes as the file name?
...
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
CartNum = String(0, 8) '''RETURNS NOTHING Get InputFileIndex, 56, CartNum
"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!)
•
•
•
•
How do I check for four bytes in a row?
...
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
'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 ...
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
![]() |
Similar Threads
- Reading a Binary file using Visual Basic (Visual Basic 4 / 5 / 6)
- Need help with Binary files and data types in VB6 (Visual Basic 4 / 5 / 6)
- problems with reading video files (Windows NT / 2000 / XP)
- Reading Binary files via stdin (C++)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: about inputBox
- Next Thread: How to set Database Applicaion path to crystal Reports
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age application basic beginner birth bmp calculator cd cells.find click client code college component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report save search sendbyte sites sort sql sql2008 sqlserver subroutine tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows





