I'm trying to do an hex editor in C#. My plan is to read a file byte by byte and display the byte in a text box.
I have the code bellow but it works very slow. Does anyone know why it works so slow ??
C#
/// <summary>
/// Opens a file chooser.
/// </summary>
///
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog.ShowDialog();
string fileName = openFileDialog.FileName;
FileStream fileReader = new FileStream(fileName, FileMode.Open, FileAccess.Read);
long length = fileReader.Length;
long position = 0;while (position < length){
position++;
textBox.Text += fileReader.ReadByte() + " ";
}
}
Best regards
Oscar Rivera21 pointsPosted May 6, 2007, 10:15 AM EDT
StringBuilder buffer = new StringBuilder();
while (position < length){
position++;
buffer.Append(fileReader.ReadByte() + " ");
}
textBox.Text = buffer.ToString();
of Course you can do something like this:
byte[] byteArray=new byte[length];
fileReader.Read(byteArray, 0, length);
textBox.Text = System.Text.ASCIIEncoding.ASCII.GetString(byteArray);
but this give you a string without the spaces that you want between bytes and be carefully
with System.Text.ASCIIEncoding.ASCII.GetString()