Im having some general confusion with encoding on a little tool I'm writing.
First of all I apologise that the following code is a little butchered but of the code I have written so far, it's the closest to actually working.
If I use the following code:
/*create file*/
FileStream fileS = new FileStream(filename + ".ppm", FileMode.Create, FileAccess.ReadWrite, FileShare.None, 8, FileOptions.None);
/*create a binary writer*/
BinaryWriter bWriter = new BinaryWriter(fileS, Encoding.ASCII);
/*write ppm header*/
string buffer = "P6 ";
bWriter.Write(buffer.ToCharArray(), 0, buffer.Length);
buffer = width.ToString() + " ";
bWriter.Write(buffer.ToCharArray(), 0, buffer.Length);
buffer = height.ToString() + " ";
bWriter.Write(buffer.ToCharArray(), 0, buffer.Length);
buffer = "255 ";
bWriter.Write(buffer.ToCharArray(), 0, buffer.Length);
/*write data out*/
byte[] messageByte = Encoding.UTF8.GetBytes(ppmDataBox.Text);
bWriter.Write(messageByte, 0, messageByte.Length);
/*close writer and bWriter*/
bWriter.Close();
fileS.Close();
Then what I get is a file saved in UTF-8 format, if I open that file and re-save it as ASCII I get the PPM I expect.
However if I change the line:
byte[] messageByte = Encoding.UTF8.GetBytes(ppmDataBox.Text);
to
byte[] messageByte = Encoding.ASCII.GetBytes(ppmDataBox.Text);
Then I do get a file saved in ASCII format but the file is wrong, the colours are wrong and basically the data in the file does not match the data in the text box.
I am assuming that the textbox is in UTF-8 and the data I am pasting into it is actually ASCII format/characters and I first need to convert that ASCII into its corresponding UTF-8...(aka be the UTF-8 version of those characters). However if I'm totally honest this is my first venture into the world of encoding and I'm completely clueless. So please let me know if I'm talking rubbish.
Here is a sample of the kind of data i'm pasting into the text box:
ÿÿ ÿÿ ÿÿ ÿÿ aa aa aa ÿÿ ÿÿ ÿÿ ÿÿ ÿÿ ÿÿ ÿÿ ÿÿ ÿÿ ÿÿ ÿ
it is meant to be yellow with little black squares everywhere, but its coming out green and when the file is created in ASCII format the data ends up looking like this:
?? ?? ?? ?? aa aa aa ?? ?? ?? ??