If the file is ASCII, then each byte IS the ASCII code. To print the value of the byte to, say, a label, is as simple as this.
If you have read your file into byte[] file;
label1.Text = file[1].ToString();
To change the second letter to A:
file[1] = (byte)'A';
Or
file[1] = (byte)(int)'A';
I'm not sure, I don't have C# on my Mac to test.
But seriously, if it is a text file, you are better reading it in as text, not as a byte[]
. And you would probably want to manipulate it using a StringBuilder
Firstly, to read it in as a string:
// Read the file as one string.
System.IO.StreamReader myFile =
new System.IO.StreamReader("c:\\test.txt");
string myString = myFile.ReadToEnd();
myFile.Close();
And this will work if the file is unicode as well.
Then, you can get the Unicode values (which for most latin characters is the same as the ASCII value) like so: int value = (int)myString[5];
or so.
You can then write back to a file like so:
System.IO.File.WriteAllText("c:\\test.txt", myString);
If you are going to do heavy modifications on the text, you should use a StringBuilder, otherwise, normal string operations would be fine.