Question Binary file to txt

Joined
Oct 9, 2009
Messages
14
Programming Experience
Beginner
Hello,

I have a Binary file that has data represented in 1604 bytes. each set of data within these groups is composed of short (2 byte) numbers. I would like to creat a vb.net program that takes this binary file then converts it to numbers, and then places it within a new text file. How would I accomplish this?

Thank you for your input
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,538
Location
Norway
Programming Experience
10+
Open up a FileStream to the file and use a BinaryReader to read the contents.
 
Joined
Oct 9, 2009
Messages
14
Programming Experience
Beginner
My code

So I have just been working on it for the last few hours, and this is the code that I have. The problem now is that it only retrieves the first group of data, and then stops. Should I be using a loop to retrieve the rest of the data? is so how would I set that up?


Thank you very much for your input John.


Imports System.IO

Public Class form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim readStream As FileStream
Dim msg As String

Try
readStream = New FileStream("C:\Documents and Settings\data", FileMode.Open)


Dim readBinary As New BinaryReader(readStream)
msg = readBinary.ReadSByte()
Using fs As New FileStream("C:\Documents and Settings\textfile.txt", FileMode.Create)
Using w As New BinaryWriter(fs)
' Write a decimal, 2 strings, a special Unicode character
' and a char.
w.Write(msg)

End Using
End Using




readStream.Close()
Catch ex As Exception
Me.Hide()
End Try
End Sub
End Class
 
Joined
Oct 9, 2009
Messages
14
Programming Experience
Beginner
Almost there

OK, so I think I created a loop that does what I need, but the data doesnt look right for 2 reasons:

1. there is a box between every integer.
2. My signals store the data in 2 byte numbers, and Sbyte assumes they are 1 byte. how do correct these problems?


Imports System.IO

Public Class form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim readStream As FileStream
Dim msg As String

Try
readStream = New FileStream("C:\Documents and Settings\file", FileMode.Open)

Dim readBinary As New BinaryReader(readStream)

Using fs As New FileStream("C:\Documents and Settings\file.txt", FileMode.Create)
Using w As New BinaryWriter(fs)

Dim i As Integer
For i = 1 To 9000
msg = readBinary.ReadSByte
w.Write(msg)
i = i + 1
Next


End Using
End Using


readStream.Close()
Catch ex As Exception
Me.Hide()
End Try
End Sub
End Class
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,538
Location
Norway
Programming Experience
10+
Joined
Oct 9, 2009
Messages
14
Programming Experience
Beginner
Almost there

OK so I tried that and it seemed to work well. The only problem now is that I expected to see negative numbers, and my output is all positive. Do you have any recommendations for that? Thank you John.


CODE:

Imports System.IO

Public Class form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim readStream As FileStream
Dim msg As Int16

Try
readStream = New FileStream("C:\Documents and Settings\TEXTIN", FileMode.Open)

Dim readBinary As New BinaryReader(readStream)

Using fs As New FileStream("C:\Documents and Settings\TEXTOUT.txt", FileMode.Create)
Using w As New BinaryWriter(fs)

Dim i As Integer
For i = 1 To 9000
msg = readBinary.ReadInt16
w.Write(msg)
i = i + 1
Next
End Using
End Using

readStream.Close()
Catch ex As Exception
Me.Hide()
End Try
End Sub
End Class
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,538
Location
Norway
Programming Experience
10+
With that code the input file and output file will be identical (for up to first 18000 bytes). Since you have named the output file with .txt extension I suspect you want to write a text file, to do that you can use the StreamWriter instead.

or call w.Write(msg.ToString) to write string content, but that will still make a binary file (with a length prefix for each string).
 
Joined
Oct 9, 2009
Messages
14
Programming Experience
Beginner
OK here we go,

Sorry about all of this confusion, but by the time I am don this will be the first program I ever wrote.


Below is the new code I am using. It is giving me data that is 4 digits long. I am expecting this data to me no more than 3 digits long (max=999, but on the bright side of things, I am receiving negative values!). Just to reiterate though, I am trying to convert each 2 byte packet into one 3-digit integer. Thank you so much for your help John.

***Just as a side note though, I would like to graph this data when I am done, so I placed it into a .txt file, but any file format will do.






Imports System.IO
Imports System.Text

Public Class form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim readStream As FileStream
Dim msg As Byte()
Dim s As Short


Try
readStream = New FileStream("C:\Documents and Settings\Binaryinput", FileMode.Open)

Dim readBinary As New BinaryReader(readStream)

Using fs As New FileStream("C:\Documents and Settings\textoutput.txt", FileMode.Create)
Using w As New TextWriterTraceListener(fs)

Dim counter As Short
For counter = 0 To 16040
msg = readBinary.ReadBytes(2)
s = BitConverter.ToInt16(msg, 0)
w.WriteLine(s)

Next


End Using
End Using


readStream.Close()
Catch ex As Exception
' Me.Hide()
End Try
End Sub
End Class
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,538
Location
Norway
Programming Experience
10+
It is giving me data that is 4 digits long. I am expecting this data to me no more than 3 digits long
Signed 16bit numbers have range -32,768 through 32,767. Either you expect wrong or read the wrong data.
 
Top Bottom