We help IT Professionals succeed at work.

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

ray_code
ray_code used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Shahan AyyubSenior Software Engineer

Commented:
I am getting correct result from your given code, and should work correctly too. Did you do modification some where ??

I had compare each and every byte this way:

            for (int i = 0; i < buff.Length - 1; i++)
            {
                Console.WriteLine(buff[i] + "|" + prgbk2bytes[i]);
            }

Every byte looks same.
Top Expert 2010
Commented:

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:

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);

Open in new window

Author

Commented:
Thanks, right on.