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)
NextThen 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.
22 Answers
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 SubAny cautions or additions? Thanks so much!
2In 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 SubBinaryReader has other methods besides the ones shown here.