Wibs2

Hi,

I am trying to convert a comma separated values text file to binary.

What I have is a text file containing 256 numbers like so:

6,6,6,7,7,8,8,9,9,10,10, etc

What I need is a binary file containing 256 bytes, so when I open it in a hex viewer it reads:

06 06 06 07 07 08 08 09 09 0A 0A etc

I have tried staring at this problem, but it doesn't help at all. I just can't think whether I am trying to convert integers into hex, or strings into hex. Please get me going. Also a nudge as to how to do the same in the reverse direction.

Many thanks,
Wibs


Re: Visual Basic Express Edition csv to binary?

Tall Dude

Dim SourceData As String = "6,6,6,7,7,8,8,9,9,10,10"

Private Sub Button2_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button2.Click

Dim SourceStrArray() As String = Split(SourceData, ",")

Dim ByteBufferArray(SourceStrArray.Length - 1) As Byte

For x As Integer = 0 To SourceStrArray.Length - 1

ByteBufferArray(x) = CByte(Val(SourceStrArray(x)))

Next

My.Computer.FileSystem.WriteAllBytes _

("C:\test.bin", ByteBufferArray, False)

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button3.Click

Dim ReadBytesArray() As Byte = _

My.Computer.FileSystem.ReadAllBytes("c:\test.bin")

Dim ReadDelimitedString As String = ""

For x As Integer = 0 To ReadBytesArray.Length - 1

ReadDelimitedString += ReadBytesArray(x).ToString

If x <> ReadBytesArray.Length - 1 Then

ReadDelimitedString += ","

End If

Next

MsgBox("Binary file opened and restored as" _

& vbCrLf & "this string: " & ReadDelimitedString)

End Sub






Re: Visual Basic Express Edition csv to binary?

Wibs2

Many thanks Tall Dude, just what I needed. I modified the first block slightly so I could read in the source file directly:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim SourceData As String = My.Computer.FileSystem.ReadAllText("c:\test.csv")

Dim SourceStrArray() As String = Split(SourceData, ",")

Dim ByteBufferArray(SourceStrArray.Length - 1) As Byte

For x As Integer = 0 To SourceStrArray.Length - 1

ByteBufferArray(x) = CByte(Val(SourceStrArray(x)))

Next

My.Computer.FileSystem.WriteAllBytes("C:\test.raw", ByteBufferArray, False)
End Sub

and the second slightly so I could output the data back to a csv file again:

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

Dim ReadBytesArray() As Byte = My.Computer.FileSystem.ReadAllBytes("c:\test.raw")

Dim ReadDelimitedString As String = ""

For x As Integer = 0 To ReadBytesArray.Length - 1

ReadDelimitedString += ReadBytesArray(x).ToString

If x <> ReadBytesArray.Length - 1 Then

ReadDelimitedString += ","

End If

Next

MsgBox("Binary file opened and restored as" & vbCrLf & "this string: " & ReadDelimitedString)
My.Computer.FileSystem.WriteAllText("c:\test2.csv", ReadDelimitedString, False)
End Sub


The RAW file was perfect. The test2.csv contained all the comma separated values, but also picked a mysterious three extra bytes at the beginning:

EF BB BF 36 2C 36 2C 36 2C

Any idea where that EF BB BF came from

Thanks again Smile




Re: Visual Basic Express Edition csv to binary?

Tall Dude

WriteAllText added a Byte Order Marker (BOM)

(The 3 extra bytes)

Whereas WriteAllBytes did not.

Also, just to nitpick, you are storing the file as binary (bin)

not as a real Comma Separated Value (csv).

It is only held as a comma separed value inside of the program.

So, a more apt file extension would be 'bin'.






Re: Visual Basic Express Edition csv to binary?

Wibs2

Thanks again Tall Dude (and sorry for the late thank you, been working at the North Pole all last week).

I don't fully understand why WriteAllText results in me storing the file as binary (bin), could you explain that a little for me.

If I want to get back exactly the same *.csv as I started with, what would be the best way to write the data to file (and get rid of the BOM, which the original *.csv file did not have)

Thanks again

Wibs




Re: Visual Basic Express Edition csv to binary?

Wibs2

Wibs2 wrote:
Thanks again Tall Dude (and sorry for the late thank you, been working at the North Pole all last week).

I don't fully understand why WriteAllText results in me storing the file as binary (bin), could you explain that a little for me.

If I want to get back exactly the same *.csv as I started with, what would be the best way to write the data to file (and get rid of the BOM, which the original *.csv file did not have)

Thanks again

Wibs

Found it! Just one parameter missing from the WriteAllText command. The line should read:
My.Computer.FileSystem.WriteAllText("c:\test2.csv", ReadDelimitedString, False, System.Text.Encoding.ASCII)

If the encoding parameter is not stated the default encoding of UTF-8 is used, and the 3 byte BOM is written. Setting the encoding to ASCII omits the BOM. It took me nearly 2 hours of searching through the MSDN knowledge base articles, and following all the links for writing text files, encoding, system.text.encoding, etc and getting nowhere, as no example that used the encoding was given. In the end I Googled for My.Computer.FileSystem.WriteAllText & encoding, and found in two mins a chap who had exactly the same problem, and giving his code for solving it.

Hope this helps someone else.

Wibs