Question : C# Reading file into bytes, then converting it to string, then converting it back to bytes

Hi, I seem to have a problem when reading file into bytes, then converting it to string, then converting it back to bytes.

The code is:

byte[] buff = null;
FileStream fs = new FileStream(System.IO.Path.GetTempPath() + "/myprogram.exe", FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(System.IO.Path.GetTempPath() + "/myprogram.exe").Length;
buff = br.ReadBytes((int)numBytes);

 System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

string buffasstring = encoding.GetString(buff);
byte[] prgbk2bytes= encoding.GetBytes(buffasstring);


--
Why are prgbk2bytes bytes are diffrent to the orignal program bytes? How can I get the original program bytes back after converting it to string.

Many thanks

Answer : C# Reading file into bytes, then converting it to string, then converting it back to bytes


ray_code, I tried your code on my .exe file. As you said the bytes before and after differ.

The reason why they differ:

Below is a line from msdn (Understanding Encodings: http://msdn.microsoft.com/en-us/library/ms404377.aspx)

"•If your application has content that is not strictly ASCII and encodes it with ASCIIEncoding, each non-ASCII character encodes as a question mark ("?"). If the application then decodes this data, the information is lost."

Obvioursly the byte data from the .exe file is "not ASCII" and information is lost.

What is the solution for this:

Below is a line from msdn (Encoding class: http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx)

"If your application must encode arbitrary binary data into text, it should use a protocol such as uuencode, which is implemented by methods such as Convert.ToBase64CharArray."

Obviously the byte data from the .exe file is arbitrary binary data and you should consider using Base64 encoding instead as shown in the code below:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
byte[] buff = null;
FileStream fs = new FileStream(System.IO.Path.GetTempPath() + "/myprogram.exe", FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(System.IO.Path.GetTempPath() + "/myprogram.exe").Length;
buff = br.ReadBytes((int)numBytes);

//System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

string buffasstring = Convert.ToBase64String(buff);
byte[] prgbk2bytes= Convert.FromBase64String(buffasstring);
Random Solutions  
 
programming4us programming4us