Reputation: 4657
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! :D
Upvotes: 2
Views: 574
Reputation: 26446
The StreamReader
is going to read the file as if it's a text, which it isn't. So yes, you'll have to use theBinaryReader
or FileStream
to read your data. In fact the BinaryReader
is redundant here. The FileStream
will work on it's own. Use FileStream.Read
. Since your file appears short enough to put into a string, the following code should work for you:
using (FileStream fs = new new FileStream(mergedFile, FileAccess.Read))
{
int length = (int) fs.Length;
byte[] data = new byte[length];
fs.Read(data, 0, length);
// convert to string here... TBD
}
Next you want to convert your binary data into a string. This depends on on how you want to represent this string. If it's ASCII (and you want each byte to convert into a char), you would do something like this:
string myString = System.Text.Encoding.UTF8.GetString( data );
Upvotes: 2
Reputation: 700870
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.
Upvotes: 3
Reputation: 185703
Your second approach is better. While the first approach COULD work, you will likely run into encoding issues.
If you need to insert the data into an XML document, use Convert.ToBase64String(yourByteArray);
. To get a byte[]
back from the string, use Convert.FromBase64String(yourStringFromXML);
Upvotes: 1