简体   繁体   English

比较两个ByteArrays C#

[英]Comparing two ByteArrays C#

I have a test that uploads a bytearray (image - jpg) to our database (Sql Server FileStream) and then retrieves it through a HttpHandler. 我有一个测试,将字节数组(image-jpg)上传到我们的数据库(Sql Server FileStream),然后通过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 第一个byteArray:255、216、255、224,从这里开始它们是相同的

Second byteArray: 63, 63, 63, 63 ... 第二个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 '?'. 如果您想知道为什么会这样,这是因为ASCII编码无法处理128以上的字符。前四个字符被转换为'?'。

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. 为此最好使用十六进制表示法。

According to the Wikipedia article , those first four bytes are: 根据Wikipedia的文章 ,前四个字节为:

  • 0xFFD8 Start Of Image 0xFFD8图像开始
  • 0xFFEn Application-specific marker 0xFFEn特定于应用程序的标记

Yours is 0xFFE0, which is the APP0 marker, required after the Start-Of-Image. 您的值为0xFFE0,它是APP0标记,在映像开始后需要。 See www.w3.org/Graphics/JPEG/jfif3.pdf , section entitled "APP0 marker used to identify JPEG FIF." 参见www.w3.org/Graphics/JPEG/jfif3.pdf ,标题为“用于识别JPEG FIF的APP0标记”的部分。

Writer class is a text encoding stream, by the sound of it, you are using a StreamWriter which is an implementation of System.IO.TextWriter. Writer类是一个文本编码流,就其声音而言,您正在使用StreamWriter,该流是System.IO.TextWriter的实现。 This is incorrect. 这是不正确的。 You should be using a binary stream to write the data in its native format. 您应该使用二进制流以其本机格式写入数据。

Hope this helps gives you the hint in the direction you are looking for, Best regards, Tom. 希望这对您所寻找的方向有所帮助,最好的问候,汤姆。

An unmodified SimpleWorkerRequest is unsuitable for receiving binary data. 未经修改的SimpleWorkerRequest不适合接收二进制数据。 Let me quote from MSDN (highlighting by me): 让我引用MSDN (由我重点介绍):

To achieve richer functionality, such as providing posted content and headers and capturing the response headers or response body as binary data , you should extend SimpleWorkerRequest and override the appropriate HttpWorkerRequest methods. 为了实现更丰富的功能,例如提供发布的内容和标头以及响应标头或响应主体 捕获 为二进制数据 ,您应该扩展SimpleWorkerRequest并重写适当的HttpWorkerRequest方法。

If your goal is simply to get it working and are able to change the database scheme, you could go with this post: http://blogs.msdn.com/domgreen/archive/2009/09/06/comparing-two-images-in-c.aspx 如果您的目标只是使其工作并能够更改数据库方案,则可以阅读以下文章: http : //blogs.msdn.com/domgreen/archive/2009/09/06/comparing-two-images -in-c.aspx

The author uses base64 encoding to compare existing images, but you could use the encoding to store the image in the db much the same way. 作者使用base64编码比较现有图像,但是您可以使用该编码将图像存储在db中的方式几乎相同。

br, Marcel br,马塞尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM