5

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 Answer 1

16

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
Sign up to request clarification or add additional context in comments.

3 Comments

@Brad: because you can then use Convert.FromBase64String to get it back again later...
So the idea is then just use the same encoding and the Base 64 is the most generic in comparsion to UTF8, 16, etc..?
@Brad: They're not the same thing. Encoding.UTF8 etc are used to represent arbitrary text as bytes... Base64 is used to represent arbitrary binary data as text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.