I have a test that uploads a bytearray (image – jpg) to our database (Sql Server FileStream) and then retrieves it through a HttpHandler. Next I compare the two.
Now, they are almost the same except for the first four bytes…
What’s in those first four bytes?
First byteArray : 255, 216, 255, 224, from here on they are the same
Second byteArray: 63, 63, 63, 63 …
When retrieved from the db by the handler, the string is converted to a bytearray like this
Update
StringWriter writer;
SimpleWorkerRequest worker;
writer = new StringWriter();
worker = new SimpleWorkerRequest(page, query, writer);
HttpRuntime.ProcessRequest(worker);
writer.Flush();
var encoding=new ASCIIEncoding();
var blob = encoding.GetBytes(writer.GetStringBuilder().ToString());
return blob;
If you want to know why this happens, this is because ASCII encoding can’t handle characters above 128. The first four characters are converted to ‘?’.
Since you have an image in the byte array, you shouldn’t try to convert it to text in order to compare the two arrays. For comparison’s sake, you should iterate through all bytes and print their values. It would be better to use hex notation for this.