0

I'm trying to create a simple Hex Editor with C#. For this I'm writing the file into an byte-array, which works fine. But as soon as I put out the bytes to a Textbox in form of a string, the overall performance of the program becomes pretty bad. For example a 190kb file takes about 40 seconds, till it is displayed in the textbox. While that the program is not responding.

The function:

void open()
    {
        fullstring = "";

        OpenFileDialog op = new OpenFileDialog();
        op.ShowDialog();
        file = op.FileName;

        byte[] fileB = File.ReadAllBytes(file);

        long b = fileB.Length;

        for (int i = 0; i < fileB.Length; i++)
        {
            fullstring = fullstring + fileB[i].ToString("X") + "  ";
        }

        textBox9.Text = fullstring;
    }

Is there a way to improve performance in this function?

tomatocake
  • 61
  • 1
  • 3
  • Really duplicate of 2 questions, I think [how to convert byte array to hex](http://stackoverflow.com/questions/623104/byte-to-hex-string) is more helpful one, but also check out http://stackoverflow.com/questions/10341188/string-concatenation-using-operator and especially links to Eric Lippert's [blog](http://ericlippert.com/2013/06/24/string-concatenation-behind-the-scenes-part-two/) – Alexei Levenkov Oct 31 '14 at 02:40

1 Answers1

0

Take a look at this post How do you convert Byte Array to Hexadecimal String, and vice versa? You can use the code there to output your byte array to text file. One problem you have in your code is that you are using String concatenation instead of StringBuilder. It is better to use StringBuilder otherwise the performance degrades.

Community
  • 1
  • 1
Max
  • 458
  • 3
  • 7