C# – Binary Data vs Character Data confusion


Ok, so I have a function that takes the path to an image file and is supposed to return the binary data as a string. The caller then inserts the returned string into the inner text of an XML element.

Is this correct?:

string dataAsString = "";
using (StreamReader sr = new StreamReader(new FileStream(mergedFile, FileMode.Open, FileAccess.Read, FileShare.Read, 2048, FileOptions.DeleteOnClose)))         
dataAsString = sr.ReadToEnd();  
return dataAsString;

This returns something that looks like it might legitimately be binary data but if I cut and paste the contents out of the target file, paste it into a new test tif file and then attempt to open the tif image, it is unhappy with me… so I suspect something is wrong with the way I'm reading/writing the data.

Must I really do something like this?

using (BinaryReader br = new BinaryReader(new FileStream(mergedFile, FileMode.Open, FileAccess.Read, FileShare.Read, 1024, FileOptions.None)))      
  int blockReadSz = 2048;
  int bytesToRead = (int)br.BaseStream.Length;
  int bytesRead = 0;
  byte[] data = new byte[bytesToRead];
  while (bytesRead < bytesToRead)         
    bytesRead += br.Read(data, bytesRead, blockReadSz);       

And if so, how do I get a string out of the byte[] once I'm done reading the file in?

Thanks for any help! 😀

Best Solution

What you are asking for doesn't really make sense. You can't read binary data into a string.

You can easily read the data into a byte array without using a BinaryReader:

byte[] data = File.ReadAllBytes(mergedFile);

If you want to put the data in an XML document, you have to represent it as text somehow, for example using base64 encoding:

string text = Convert.ToBase64String(data);

You can't just decode the data into a string, as it's not a string that was encoded in the first place. Whatever encoding you use, the binary data can always contain byte sequences that doesn't represent anything that would be produced by encoding text.