1

This is what I use in VB5. How can I do the same thing in VB.net (2015)?

With all the variables dimensioned the following VB5 code reads the first four bytes in a binary file opened as #2 to fill the li(4) array.

For i = 1 To 4
mychar = InputB(1, #2) 'Get one character.
li(i) = AscB(mychar)
Next

Then I call my liconvert(a,b,c,d) function to get the long integer number represented by the first four bytes in the file and return that number as “t”

t = Val(liconvert(li(1), li(2), li(3), li(4)))

What I would do from here takes a lot more code. I just need to get this far.

2
  • 1
    msdn.microsoft.com/en-us/library/… Commented Mar 15, 2016 at 2:19
  • Once you have called FileStream.Read to get the first four bytes from the file, you can use the BitConverter class to convert those four bytes to a long. Commented Mar 15, 2016 at 3:44

2 Answers 2

2

With the start I got from you I did more looking and found this code which seems to do exactly what I need.

Public Sub ReadBinaryII()
' Get the file name.
Dim file_name As String = "xxx.xxx"
' Open the file.
Dim fs As New FileStream(file_name, FileMode.Open)
' Create a BinaryReader for the FileStream.
Dim binary_reader As New BinaryReader(fs)
fs.Position = 0
' Read the data as a byte array.
Dim bytes() As Byte = binary_reader.ReadBytes(20)
' Print the byte data to a text box
myForm.Txt1.Text = bytes(0) & "/" & bytes(1) & "/" & bytes(2) & "/" & bytes(3)
binary_reader.Close()
fs.Dispose()
End Sub

Any cautions or additions? Thanks so much!

Sign up to request clarification or add additional context in comments.

2 Comments

I would recommend creating your readers and file streams inside a Using block so they are automatically closed/disposed when you are finished with them.
Thanks Chris. I'll do that.
0

In addition to jmcilhinney's comment, you can use the BinaryReader to read values from a file. See this example:

Public Sub ReadBinary()
    Using strm As New FileStream("c:\test\filename.bin", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
        Using rdr As New BinaryReader(strm)
            'Read integer and byte vaules from the file
            Dim i As Integer = rdr.ReadInt32()
            Dim l As Long = rdr.ReadInt64()
            Dim b As Byte = rdr.ReadByte()

            'Read 100 bytes from the file
            Dim bytes(99) As Byte
            Dim bytesRead As Integer = rdr.Read(bytes, 0, 100)
        End Using
    End Using
End Sub

BinaryReader has other methods besides the ones shown here.

4 Comments

Thanks. Didn't quite work. I copied the code replacing the filename with my own path/file and added a text box to display the value of "bytesRead". When I called the Sub the textbox just shows the value "100". If I change the "(bytes,0,100)" to "(bytes,0,4)" the text box shows "4". What I need is a way for the routine to read the file and get the ASCII values of each byte in the file sequentially. For example if the Binary file started out as "aq7B...." I could fill an array with the ASCII equivalents which are 97,113,7,66. I'm sure I'm close but missing something.
Ok now I see what I've done. My text box is just displaying the number of bytes read from the file, DUH!. How do I get the ASCII values of the bytes into an array?
Ok. I've got it partially figured out. The bytes(99) array does hold the characters from the binary file. That's what I need. However, for some reason byte(0) is actually the 15th byte in my file. I'm trying to figure that one out.
In the example code, reading the Int32 advances the file pointer 4 bytes, the Int64 advances it by 8, and the Byte would advance it another 1 byte so by the time the Read is called for the array, the file pointer should be on the 14th byte.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.