如何比较两个字节数组[重复]

时间:2022-07-19 12:07:48

This question already has an answer here:

这个问题在这里已有答案:

I have two byte arrays with the exact same content. I tried:

我有两个字节数组,内容完全相同。我试过了:

if (bytearray1 == bytearray2) {...} else {...}

and

if (Array.Equals(bytearray1, bytearray2)) {....} else {...}

All time it goes to the else! I don't know why! I checked both arrays manually several times!!!

一直到别的地方!我不知道为什么!我手动检查了两个阵列几次!

4 个解决方案

#1


65  

Try using the SequenceEqual extension method. For example:

尝试使用SequenceEqual扩展方法。例如:

byte[] a1 = new byte[] { 1, 2, 3 };
byte[] a2 = new byte[] { 1, 2, 3 };
bool areEqual = a1.SequenceEqual(a2); // true

#2


12  

The == operator compares by reference; those are two different instances.

==运算符通过引用进行比较;这是两个不同的例子。

Array.Equals is really Object.Equals, which calls the instances Equals method.
Since arrays do not override Equals(), this too compares by reference.

Array.Equals实际上是Object.Equals,它调用实例Equals方法。由于数组不会覆盖Equals(),因此也通过引用进行比较。

Instead, you should call the LINQ SequenceEqual() method.

相反,您应该调用LINQ SequenceEqual()方法。

#3


8  

Both the == operator and the Equals method will test reference equality. Since you have two separate arrays, they will never be equal.

==运算符和Equals方法都将测试引用相等性。由于你有两个独立的数组,它们永远不会相等。

Since you want to test that both arrays have the same content in the same order, try using the SequenceEqual method instead.

由于您要测试两个阵列具有相同顺序的相同内容,请尝试使用SequenceEqual方法。

#4


-4  

As an alternative, if you are not comfortable using LINQ you can use the System.Convert class...

作为替代方案,如果您不习惯使用LINQ,可以使用System.Convert类...

byte[] a1;
byte[] a2;

if (System.Convert.ToBase64String(a1) == System.Convert.ToBase64String(a2)) {
    doSomething();
}
else {
    doSomethingElse();
}

#1


65  

Try using the SequenceEqual extension method. For example:

尝试使用SequenceEqual扩展方法。例如:

byte[] a1 = new byte[] { 1, 2, 3 };
byte[] a2 = new byte[] { 1, 2, 3 };
bool areEqual = a1.SequenceEqual(a2); // true

#2


12  

The == operator compares by reference; those are two different instances.

==运算符通过引用进行比较;这是两个不同的例子。

Array.Equals is really Object.Equals, which calls the instances Equals method.
Since arrays do not override Equals(), this too compares by reference.

Array.Equals实际上是Object.Equals,它调用实例Equals方法。由于数组不会覆盖Equals(),因此也通过引用进行比较。

Instead, you should call the LINQ SequenceEqual() method.

相反,您应该调用LINQ SequenceEqual()方法。

#3


8  

Both the == operator and the Equals method will test reference equality. Since you have two separate arrays, they will never be equal.

==运算符和Equals方法都将测试引用相等性。由于你有两个独立的数组,它们永远不会相等。

Since you want to test that both arrays have the same content in the same order, try using the SequenceEqual method instead.

由于您要测试两个阵列具有相同顺序的相同内容,请尝试使用SequenceEqual方法。

#4


-4  

As an alternative, if you are not comfortable using LINQ you can use the System.Convert class...

作为替代方案,如果您不习惯使用LINQ,可以使用System.Convert类...

byte[] a1;
byte[] a2;

if (System.Convert.ToBase64String(a1) == System.Convert.ToBase64String(a2)) {
    doSomething();
}
else {
    doSomethingElse();
}