6

I am using the following function to convert binary data to text:

public string BinaryToText(byte[] data)
{
     MemoryStream stream = new MemoryStream(data);
     StreamReader reader = new StreamReader(stream, encoding.UTF8);
     string text = reader.ReadTod();
     return text;
}

But I get an OutOfMemoryException for 30Mb data.

How can I solve this problem and convert data larger than 100Mb, using this or any better method?

4
  • do you intend to send this to a client> Commented Jan 14, 2013 at 14:26
  • If that data isn't really UTF8, then what you're doing is wrong anyway! Is the original data really UTF8? Commented Jan 14, 2013 at 14:27
  • possible duplicate stackoverflow.com/questions/1003275/byte-to-string-in-c-sharp Commented Jan 14, 2013 at 14:53
  • If you data is UTF8 then use System.Text.Encoding.UTF8.GetString Commented Jan 14, 2013 at 14:55

1 Answer 1

8

Try this instead:

public string BinaryToText(byte[] data)
{
    return Encoding.UTF8.GetString(data);
}

It will consume less memory. If it still runs out of memory, you'll need to read it in chunks - but how you are using the data will determine if that is possible. What are you doing with the returned string?

Also I will reiterate my earlier question: Is the input data really UTF8 data?

If you can handle the data being returned as multiple strings, you can do something like this:

public IEnumerable<string> BinaryToStrings(byte[] data, int maxStringLength)
{
    var buffer = new char[maxStringLength];

    using (MemoryStream stream = new MemoryStream(data))
    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
    {
        while (true)
        {
            int count = reader.Read(buffer, 0, maxStringLength);

            if (count == 0)
            {
                break;
            }

            yield return new string(buffer, 0, count);
        }
    }
}

Then you can call that in a foreach loop like so:

foreach (string chunk in BinaryToStrings(data, 1024))
{
    // Do something with 'chunk'...
}
Sign up to request clarification or add additional context in comments.

Comments

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.