Adam
Adam

Reputation: 735

String back into byte array - not working

I have a stream that gets converted into a byte array.

I then take that bye array and turn it into a string.

When I try to turn that string back into a byte array it is not correct...see the code below.

     private void Parse(Stream stream, Encoding encoding)
     {
        // Read the stream into a byte array
        byte[] allData = ToByteArray(stream);

        // Copy to a string for header parsing
        string allContent = encoding.GetString(allData);

        //This does not convert back right - just for demo purposes, not how the code is used
        allData = encoding.GetBytes(allContent);
    }

    private byte[] ToByteArray(Stream stream)
    {
        byte[] buffer = new byte[32768];
        using (MemoryStream ms = new MemoryStream())
        {
            while (true)
            {
                int read = stream.Read(buffer, 0, buffer.Length);
                if (read <= 0)
                    return ms.ToArray();
                ms.Write(buffer, 0, read);
            }
        }
    }

Upvotes: 4

Views: 1634

Answers (2)

rsbarro
rsbarro

Reputation: 27339

I think changing the ToByteArray method to use a StreamReader that matches the encoding should work in this case, although without seeing more of the code I can't be certain.

private byte[] ToByteArray(Stream stream, System.Text.Encoding encoding)
{
    using(var sr = new StreamReader(stream, encoding))
    {
        return encoding.GetBytes(sr.ReadToEnd());
    }
}

EDIT
Since you're working with image data, you should use Convert.ToBase64String to convert the byte[] to a string. You can then use Convert.FromBase64String decode to convert back into a byte[]. The reason encoding.GetBytes doesn't work is because there may be some data in the byte[] that cannot be represented as a string for that encoding.

private void Parse(Stream stream, Encoding encoding)
{
    byte[] allData = ToByteArray(stream);

    string allContent = Convert.ToBase64String(allData);  

    allData = Convert.FromBase64String(allContent);
}

Upvotes: 2

Mas
Mas

Reputation: 4606

Without having more information, I'm quite certain that this is a text encoding issue. Most likely, the text encoding in the stream is different than the encoding specified as your parameter. This will result in different values at the byte level.

Here's a few good articles that explains why you're seeing what you're seeing.

Upvotes: 2

Related Questions