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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 117k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer class Program { static void Main(string[] args) { Timer timer… May 11, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer Use GROUP_CONCAT SELECT GROUP_CONCAT(bar) FROM TABLE GROUP BY foo; May 11, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer Your code doesn't work because the function is not returning… May 11, 2026 at 10:50 pm

Related Questions

I have been trying to track down weird problems with my mod_wsgi/Python web application.
Ok, so that title probably doesn't explain my question well. Hopefully this makes sense.
I'm Currently reading two excellent books Working Effectively with Legacy Code and Clean Code.
Ok so I realize that this is a pretty vague question, but bear with

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.