4

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);
            }
        }
    }
6
  • 1
    Can you show the code of ToByteArray(Stream s) pls. Commented May 30, 2011 at 6:25
  • Can you be a bit more specific than 'not correct'? Commented May 30, 2011 at 6:27
  • Which encoding do you pass to the Parse() method? And which encoding does the data in the Stream use? Commented May 30, 2011 at 6:33
  • Can you elaborate on what is different? It might be that just the first few bytes, which can contain encoding type information, are different. Commented May 30, 2011 at 6:34
  • I am using Encoding.UTF8 for the encoding Commented May 30, 2011 at 6:37

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

even though they both use the same encoding? and any idea how to make it work? Basically I am parsing a multipart/form-data which has a image in it and some values
Can you compare the two byte arrays and see what is different? Are they different lengths (not the array size itself, but the contents of the array)? Are only the first 2 bytes different? UTF-16 can be encoded as little-endian or big-endian, the first 2 bytes signal which is used: en.wikipedia.org/wiki/Byte_order_mark#cite_note-11
2

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);
}

10 Comments

When I do it this way it doesn't create the image right when trying to writeing it out to a file
@Adam Can you post more of the code then? How are you trying to write to disk?
Here is how I write the bytes to disk if (obj.FileContents != null) { int ArraySize = obj.FileContents.Length; string outputFile = @"C:\Temp\upload" + i.ToString() + ".jpg"; FileStream fs = new FileStream(outputFile, FileMode.OpenOrCreate, FileAccess.Write); fs.Write(obj.FileContents, 0, ArraySize); fs.Close(); }
And the stream comes a HTML form being submitted that is enctype="multipart/form-data"
@Adam Are you sure the image data isn't base64 encoded in the multipart/form message? If it was then you would need to decode before saving to disk.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.