Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

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

Avatar of ray_code
ray_code asked on
3 Comments1 Solution446 ViewsLast Modified:
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

Avatar of kris_per
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answers