0

I read a binary file of 900 bytes with much informations inside.
Like this:

     Dim myFile As String = txt_mydir.Text + "\MY_FILE.BIN"
     If IO.File.Exists(myFile) Then
        Dim fInfo As New FileInfo(myFile)
        Dim numBytes As Long = fInfo.Length
        Dim fStream As New FileStream(myFile, FileMode.Open, FileAccess.Read)
        Dim br As New BinaryReader(fStream)
        Dim data As Byte() = br.ReadBytes(CInt(numBytes))

All bytes finishes in bytearray 'data'.

Now I have to read a numbers written with VB6 structs into that file. Structs are mine and I know what is what and where is what. For example I need a VB.NET 'short' number which is at bytes 81 and 82. Among that I have all other basic number types to get out.

How to take out desired number of bytes from 'data' from specific location, with exact length and get a proper number from it (short, int, double...)?

3
  • Can you give an example of what you need? Commented Dec 20, 2012 at 17:49
  • How can I give example when I need example? Commented Dec 20, 2012 at 18:13
  • Examples of the array, what values you need from it? The inputs? Surely you can give some. Commented Dec 20, 2012 at 18:34

1 Answer 1

1

Use the methods of the BinaryReader to get the fields of your original struct

Dim i As Integer = br.ReadInt32()
Dim d As Double = br.ReadDouble()
Dim s As String = br.ReadString()

And so on. You must read the fields in exactly the same order as they were written to the file.

Embed it into a loop like this

While br.BaseStream.Position() <> inFile.BaseStream.Length()
    ...
End While
Sign up to request clarification or add additional context in comments.

4 Comments

Very interesting Olivier, and works. But I would have to seek somehow over half of file because I have to pass more than 200 variables which is not needed.
You can fist seek to the desired position and then use an appropriate read-method.
That can be a solution. I am new to vb.net, so can you please show how to seek to desired position?
I find ;), thank you very much "fStream.Seek(417, SeekOrigin.Begin)"

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.