Reading All File Text

Posts 12 of 2 · Page 1 of 1
Reading All File Text
I have a piece of code which may help you, It basically reads every byte of a file including unknown symbols (Converts them to "?"), Just like NotePad++.
Code:
byte[] FileBytes = File.ReadAllBytes("C:\\TextFile.txt");

            for (Int32 xx = 0; xx <= FileBytes.Length - 1; xx++)
            {
                if (FileBytes[xx] == 0) 
                {
                    FileBytes[xx] = 63;
                }
            }
            string asciiStr = System.Text.Encoding.ASCII.GetString(FileBytes);
            Control.Text = asciiStr;
Using only File.ReadAllBytes won't read unknown symbols, for example Dll files. It will just show "MZ?".
Thanks to abuckau907.
Just wanted to say .ReadAllBytes() does actually read all the bytes from the file...
Set a breakpoint and checked the byte array after the call to File.ReadAllBytes(some.dll)

^^and it has lots of data. (in vb,but using the same function)

The 'problem' (which isn't a problem, but is just how ascii works) is the Ascii.GetString() function, and the fact that any '0' byte signifies the end of a string. But you solved that by replacing every 0 byte with the value 63 ('?' character). Congrats.

As a note, "standard" ascii will only use 7 out of the 8 bits in a byte. values 0 to 127.
01111111 = 127 = largest ascii value
10000000 = 128 = too big

if (value > 127) // not standard ascii
But you don't have to worry about it because anything > 127 automatically gets displayed as a '?'

Thanks for saying thanks.
Posts 12 of 2 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?