Reading a file one byte at a time

  • Thread starter Thread starter Wesley
  • Start date Start date
W

Wesley

Hi,

I'm trying to create a module which will open a file and
read one byte at a time. I have created a form and added
four textbox's, labeled: Binary, Hex, Ascii, Dec.
I've also added two command buttons, labeled: Previous
Byte, Next Byte.

I'm wanting to write code which will open any file and
read in the first byte, then display it in the textbox's
in the format of Binary, Hex, Dex, Ascii, and use the
command buttons to move back and forth in the file. I've
also added a textbox labeled: strPath, which is the fill
path and filename to open.

I've looked at VBA help and can only find help using the
getchuck method, also searched knowledge base and found
nothing that gets me started.



..
 
Wesley said:
Hi,

I'm trying to create a module which will open a file and
read one byte at a time. I have created a form and added
four textbox's, labeled: Binary, Hex, Ascii, Dec.
I've also added two command buttons, labeled: Previous
Byte, Next Byte.

I'm wanting to write code which will open any file and
read in the first byte, then display it in the textbox's
in the format of Binary, Hex, Dex, Ascii, and use the
command buttons to move back and forth in the file. I've
also added a textbox labeled: strPath, which is the fill
path and filename to open.

I've looked at VBA help and can only find help using the
getchuck method, also searched knowledge base and found
nothing that gets me started.

Here's a snippet that should get you started.

Dim fh As Variant, x As Long, bytValue As Byte

fh = FreeFile
Open sFilename For Binary Access Read Write As #fh

Seek #fh, 1
For x = 1 To CHARS_TO_WRITE
bytValue = &H15
Put #fh, , bytValue

' to read, use Get
' Buffer = String(CHARS_TO_READ, " ")
' Get #fh, , Buffer

Next x

Close #fh
 
Be aware the the other respondent's code will write to & change you file!
(He knows that - you might not.)

Here's what I'd use:

Dim nFile as integer, bByte as byte
nfile = FreeFile
Open "your filename here" For Binary Access Read As #nFile

' then to read byte #6 (for example):
Get #nFile, 6, bByte

' then eventually:
Close #nFile

HTH,
TC
 
Thanks for both replies,

Thanks for clearing up on how to read a byte, because I
was a bit confused. I think I can find info on the
conversion from one format the the other, my question is
how to use :Get #nFile, 6, bByte to loop thru the file
byte by byte using the Previous/Next Byte Command buttons.

If I understand your code I would set the control source
of the Binary Textbox to: =bByte to display the byte in
binary format. I'm sure I can find a method to display in
Hex, Ascii, and Decimal.

Again Thanks to both of you for your replies to this post.
 
To read the file byte by byte, you would do something like this (untested):

Open ... etc.
while not eof(#nfile)
get #nfile,,bByte
wend

Note that the code above does not provide a value for the "byte number"
parameter of the 'get' statement. That is because if you do a 'get' without
that parameter, it gets the next byte after the previous one (whatever that
was). So if you read byte #6 explicitly, then say 'get #nfile,,bByte' that
would get byte #7.

As for displaying the byte value in hex & decimal, you could have two
textboxes txtHex and txtDec, and use the following code: (I'll leave the
binary option for you to do as an exercise!)

me![txtHex] = right$ ("0" & hex$(bByte), 2)
me![txtDec] = cdec(bByte)

If you put those line between the 'get' statement & the 'wend' statement,
your code would zoom through the file, displaying the values at warp speed!
So you would need some way of pausing the output, perhaps with a msgbox.

HTH,
TC
 
Back
Top