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 ??
///
{
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 RiveraPosted May 6, 2007, 2:15 PM
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()