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 7442747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:13:23+00:00 2026-05-29T11:13:23+00:00

I want to convert a binary file to a string which could be then

  • 0

I want to convert a binary file to a string which could be then converted back to the binary file.
I tried this:

byte[] byteArray = File.ReadAllBytes(@"D:\pic.png");
for (int i = 0; i < byteArray.Length; i++)
{
    textBox1.Text += (char)byteArray[i];
}

but it’s too slow, it takes about 20 seconds to convert 5KB on i5 CPU.
I noticed that notepad does the same in much less time.
Any ideas on how to do it?

Thanks

  • 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-29T11:13:24+00:00Added an answer on May 29, 2026 at 11:13 am

    If you want to be able to convert back to binary without losing any information, you shouldn’t be doing this sort of thing at all – you should use base64 encoding or something similar:

    textBox1.Text = Convert.ToBase64String(byteArray);
    

    You can then convert back using byte[] data = Convert.FromBase64String(text);. The important thing is that base64 converts arbitrary binary data to known ASCII text; all byte sequences are valid, all can be round-tripped, and as it only requires ASCII it’s friendly to many transports.

    There are four important things to take away here:

    • Don’t treat arbitrary binary data as if it were valid text in a particular encoding. Phil Haack wrote about this in a blog post recently, in response to some of my SO answers.
    • Don’t perform string concatenation in a loop; use a StringBuilder if you want to create one final string out of lots of bits, and you don’t know how many bits in advance
    • Don’t use UI properties in a loop unnecessarily – even if the previous steps were okay, it would have been better to construct the string with a loop and then do a single assignment to the Text property
    • Learn about System.Text.Encoding for the situation where you really have got encoded text; Encoding.UTF8.GetString(byteArray) would have been appropriate if this had been UTF-8-encoded data, for example
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert a primitive to a string, and I tried: myInt.toString(); This
I'm writing a Binary file converter in which I need to convert 1-6 byte
I want to convert a binary file into an array of ascii charcters .
I'm reading from a binary file and want to convert the bytes to US
I have a binary that I want to convert to standard MIME-compliant base64 string.
i want to read\write a binary file which has the following structure: The file
I have a txt(ascii) file but i wish to convert this file into binary
I am using Flex builder with sdk 3.5. I want to convert binary string
I want to convert decimal number in binary number. I'm using this method: -
I have a Binary tree for a mathematical expression(infix), i want to convert directly

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.