Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 192273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:24:44+00:00 2026-05-11T16:24:44+00:00

Ok, so I have a function that takes the path to an image file

  • 0

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! 😀

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:24:44+00:00Added an answer on May 11, 2026 at 4:24 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have an f# function that takes a string containing a sentence, what
In MATLAB there's a nice function called fileparts that takes a full file path
I have a function that takes, amongst others, a parameter declared as int privateCount
I have a function that takes a struct, and I'm trying to store its
I have a function that takes pointer to pointer to struct. struct ABC; void
I have a Python function that takes a numeric argument that must be an
I have created a function that takes a SQL command and produces output that
I have a function called FindSpecificRowValue that takes in a datatable and returns the
I have a javascript function (class) that takes a function reference as one paremter.
I have class with a member function that takes a default argument. struct Class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.