Saving Hex back to a File

Sutton2k9

Newcomer
Joined
Jan 16, 2012
i can't seem to get it to save the Hex string back to a file.
This is the code i using to open the file
Code:
        Dim builder As New StringBuilder
        Using reader As FileStream = File.OpenRead(Parth)
            Dim length As Long = reader.Length
            Do Until reader.Position = length
                If reader.Position > 0L Then
                    builder.Append(" "c)
                End If
                builder.Append(reader.ReadByte().ToString("X2"))
            Loop
        End Using
        RichTextBox1.Text = builder.ToString()
But how can i save the Hex in richtexbox1.text
Thanks Sutton
 

snarfblam

Ultimate Contributor
Joined
Jun 10, 2003
Location
USA
First, make sure you understand exactly what format the hex needs to be in. Must there be one and only one space between each value? Or can there be more spaces? Tabs? New-lines? And must there be two digits for each value? Or can values less than 0x10 be represented by a single digit? It's easier to write code when you know exactly what it's supposed to do.

Assuming that the desired format is that each value be represented as one or two digits with one and only one space between each value, and no other whitespace or other characters, the simplest way to parse the text would be to use the string.split function to split the string into an array of value strings. You can then use the Integer.Parse or Integer.TryParse method to parse each string in the array, and write the value to the file.

The problem with this approach is that, if you are dealing with enough data, you'll generate an enormous number of objects for the garbage collector to manage. If you're only dealing with smaller amounts of data, a solution that involves fewer than a dozen lines of code is probably ideal. If you find the performance to be unacceptable, then you can start looking into parsing the numeric values yourself without splitting up the string and calling Integer.Parse.
 
Top Bottom