I try to read the hexadecimal file, but nothing happens, I do it

void Read() { FileStream stream = new FileStream("C:\\test.save", FileMode.Open, FileAccess.Read); int leng = (int)stream.Length; byte[] bufer = new byte[stream.Length]; stream.Read(bufer, 0, leng); ASCIIEncoding ascii = new ASCIIEncoding(); string res = ascii.GetString(bufer, 0, leng); richTextBox1.AppendText(res); stream.Close(); } 

and it works (somehow), but the file is read for a very long time. void Read () {

 FileStream stream = new FileStream("C:\\test.save", FileMode.Open, FileAccess.Read); int leng = (int)stream.Length; byte[] bufer = new byte[stream.Length]; stream.Read(bufer, 0, leng); ASCIIEncoding ascii = new ASCIIEncoding(); char[] chars = ascii.GetChars(bufer); foreach (char ch in chars) { string symb = ch.ToString(); ichTextBox1.AppendText(symb); } stream.Close(); } 

Help me please. P.c tag for formatting C # code did not find = (

  • And what is your understanding of "hex file"? - eigenein
  • I wanted to write binary ones, but even distracted myself and wrote something wrong) If I need to upload a file .. - cyber_ua
  • Your code should do what you need. What is wrong? - eigenein
  • This is what I get as a result of <quote> 24 </ quote> and a pair of scrawl from this skydrive.live.com/… file. The second option works as it should, but as long as it reads the file, you can go to sleep)) - cyber_ua
  • 3
    The expected effect of trying to read a non-text file as a text file. And what do you expect to get? - eigenein

2 answers 2

 var buf = File.ReadAllText("filepath", Encoding.ASCII); 

The third link in the issuance of Google.

Option with replacing zeros with spaces (so that it is normally displayed in RichTextBox):

  using (var reader = new StreamReader("12345.wotreplay", Encoding.ASCII)) { var builder = new StringBuilder((int)reader.BaseStream.Length + 1); while (!reader.EndOfStream) { var chr = (char)reader.Read(); if (chr == 0) chr = ' '; builder.Append(chr); } var result = builder.ToString(); } 
  • Result: "24" and a pair of characters (which are not copied). I was looking in Google ... - cyber_ua
  • @cyber_ua: where is the result? in the buf variable? What does "not copy" mean? - VladD
  • So the encoding is not ASCII. Show your file. - nitrocaster
  • one
    Your second option works for a long time, because you do a bunch of unnecessary transformations. The curve result from your first comment was obtained because there are several zeros at the beginning of the file. The first zero byte is considered the end of the line, so the RichTextBox does not show everything else. Read the file byte-bye using StreamReader. To form a string, use StringBuilder. Before calling StringBuilder.Append (), check whether the next character is null. If yes - write space instead and read further. All this trivialism is well described on MSDN, one has only to look. - nitrocaster
  • one
    "12345.wotreplay" :) - eigenein

Just in case, an example of a simple program that implements writing and reading a binary file.

  • one
    Yes, I already figured it out, read the file correctly, but just did not output correctly ... - cyber_ua
  • one
    A classic example of C ++ nickname that moved to .NET: freeing memory, stack buffers, and so on - in a particular example, it looks ridiculous. - nitrocaster
  • In .net everything is made much easier: var bytes = File.ReadAllBytes ("source"); File.WriteAllBytes ("destination", bytes); If the file is really big, more than, say, 10 MB, do it like this: stackoverflow.com/a/2030971/276994 - VladD