0

I have a binary file "blahblah.dat" that consists of data stored in a 16byte structure. Each line in my file is 16 bytes long.

I want to read this file line by line, converting the binary data into decimal and then displaying it in a textbox.

I've had a search and can't seem to find anything suitable.


Using this code:

Using reader As New BinaryReader(File.Open("C:\Users\User\Desktop\test.dat", FileMode.Open))
        ' Loop through length of file.
        Dim pos As Integer = 0
        Dim length As Integer = reader.BaseStream.Length
        While pos < 1024
            ' Read the integer.
            For i = 0 To 15
                Dim value As Decimal = reader.ReadByte
                ' Write to screen.
                Console.WriteLine(value)
                TextBox1.Text = TextBox1.Text & value & ","
                pos += 1
            Next
            ' Add length of decimal in bytes to position.
            TextBox1.Text = TextBox1.Text & vbCrLf
        End While
    End Using

I got these results(which are what I think to be correct - I will confirm later on):

0,0,128,32,0,0,0,0,2,162,36,18,7,8,3,11,
0,3,128,32,0,0,0,0,1,138,37,18,7,8,3,11,
0,51,128,40,0,0,0,0,0,113,38,18,7,8,3,11,
0,51,128,40,0,0,0,0,3,66,38,18,7,8,3,11,
0,51,128,40,0,0,0,0,2,42,39,18,7,8,3,11,
0,51,130,40,0,0,0,0,1,18,40,18,7,8,3,11,
0,51,130,40,0,0,0,0,3,226,40,18,7,8,3,11,
0,51,130,40,0,0,0,0,2,202,41,18,7,8,3,11,
0,51,130,40,0,0,0,0,1,178,42,18,7,8,3,11,
0,51,130,40,0,0,0,0,0,154,43,18,7,8,3,11,
0,51,130,40,0,0,0,0,3,106,43,18,7,8,3,11,
0,51,130,40,0,0,0,0,2,82,44,18,7,8,3,11,
0,51,130,40,0,0,0,0,1,58,45,18,7,8,3,11,
0,51,130,40,0,0,0,0,0,34,46,18,7,8,3,11,
0,51,130,40,0,0,0,0,2,242,46,18,7,8,3,11,
0,51,130,40,0,0,0,0,1,218,47,18,7,8,3,11,
0,51,130,40,0,0,0,0,0,195,48,18,7,8,3,11,
0,51,130,40,0,0,0,0,3,146,48,18,7,8,3,11,
0,51,130,40,0,0,0,0,2,122,49,18,7,8,3,11,
0,51,130,40,0,0,0,0,1,98,50,18,7,8,3,11,
0,51,130,40,0,0,0,0,0,74,51,18,7,8,3,11,
0,51,130,40,0,0,0,0,3,26,51,18,7,8,3,11,
0,51,130,40,0,0,0,0,2,3,52,18,7,8,3,11,
0,51,130,40,0,0,0,0,0,234,53,18,7,8,3,11,
0,51,130,40,0,0,0,0,3,187,53,18,7,8,3,11,
0,51,130,40,0,0,0,0,2,163,54,18,7,8,3,11,
0,51,130,40,0,0,0,0,1,139,55,18,7,8,3,11,
0,51,130,40,0,0,0,0,0,115,56,18,7,8,3,11,
0,51,130,40,0,0,0,0,3,67,56,18,7,8,3,11,
0,51,130,40,0,0,0,0,2,43,57,18,7,8,3,11,
0,51,130,40,0,0,0,0,1,19,58,18,7,8,3,11,
0,51,130,40,0,0,0,0,3,227,58,18,7,8,3,11,
0,51,130,40,0,0,0,0,2,203,59,18,7,8,3,11,
0,51,130,40,0,0,0,0,1,179,0,19,7,8,3,11,
0,51,130,40,0,0,0,0,0,156,1,19,7,8,3,11,
0,51,130,40,0,0,0,0,3,108,1,19,7,8,3,11,
0,51,130,40,0,0,0,0,2,84,2,19,7,8,3,11,
0,0,128,32,0,0,0,0,3,172,9,19,7,8,3,11,
15,0,128,32,0,0,0,0,1,238,10,19,7,8,3,11,
15,0,128,0,0,0,0,0,0,44,11,19,7,8,3,11,
15,51,128,8,0,0,0,0,2,80,11,19,7,8,3,11,
15,51,128,8,0,0,0,0,0,151,12,19,7,8,3,11,
15,51,128,8,0,0,0,0,2,197,12,19,7,8,3,11,
15,51,128,8,0,0,0,0,1,10,13,19,7,8,3,11,
15,51,128,8,0,0,0,0,3,57,13,19,7,8,3,11,
15,51,128,8,0,0,0,0,1,140,14,19,7,8,3,11,
15,51,128,8,0,0,0,0,3,192,14,19,7,8,3,11,
15,51,128,8,0,0,0,0,2,13,15,19,7,8,3,11,
15,51,128,8,0,0,0,0,0,95,16,19,7,8,3,11,
15,51,128,8,0,0,0,0,2,145,16,19,7,8,3,11,
15,51,128,8,0,0,0,0,0,225,17,19,7,8,3,11,
15,51,128,8,0,0,0,0,3,26,17,19,7,8,3,11,
15,51,128,8,0,0,0,0,1,104,18,19,7,8,3,11,
15,51,128,8,0,0,0,0,3,152,18,19,7,8,3,11,
15,51,128,8,0,0,0,0,1,229,19,19,7,8,3,11,
15,51,128,8,0,0,0,0,0,44,20,19,7,8,3,11,
15,51,128,8,0,0,0,0,2,88,20,19,7,8,3,11,
15,51,128,8,0,0,0,0,0,159,21,19,7,8,3,11,
15,51,128,8,0,0,0,0,2,213,21,19,7,8,3,11,
15,51,128,8,0,0,0,0,1,30,22,19,7,8,3,11,
15,51,128,8,0,0,0,0,3,75,22,19,7,8,3,11,
15,51,128,8,0,0,0,0,1,145,23,19,7,8,3,11,
15,51,128,8,0,0,0,0,3,193,23,19,7,8,3,11,
15,51,128,8,0,0,0,0,2,12,24,19,7,8,3,11,

Would that be the best way to get the data or is there any way to tidy up the code?

1 Answer 1

1

Try this

Imports System.IO

Module Module1
    Sub Main()
    ' Create the reader in a Using statement.
    ' ... Use File.Open to open the existing binary file.
    Using reader As New BinaryReader(File.Open("blahblah.dat", FileMode.Open))
        ' Loop through length of file.
        Dim pos As Integer = 0
        Dim length As Integer = reader.BaseStream.Length
        While pos < length
        ' Read the integer.
        Dim value As Decimal = reader.ReadDecimal()
        ' Write to screen.
        Console.WriteLine(value)
        ' Add length of decimal in bytes to position.
        pos += 16
        End While
    End Using
    End Sub
End Module
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry I forgot to say that I'm creating this in a windows form so I would like to have the event happen when I press a button. Also is there any way to read it line by line rather than all in one go as the file is approx. 3.5mb
so call this method from inside an event
Thanks for that. I didn't get the output that I expected though. Is there a way to read the raw data from the file? It read the full line as decimal (which is what I asked for) but I need to read it as a string if possible as I need to split the data up.
try reader.ReadString();, infact of you need to convert entire binary file to string you can directly pass array of bytes from fileload to System.Text.Encoding.Unicode.GetString(bytes), which will give you a long string
I've edited my original post to show the code I've used to get my data and it is how it should be. - I now have a problem with the 9th and 10th figure, these are the lsb and msb respectively for time data (milliseconds) how would I go about using/combining these two figures to create one figure that is time in milliseconds?
|

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.