Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# byte array comparison

Tags:

c#

.net

bytearray

I have two byte arrays in C# using .NET 3.0.

What is the "most efficient" way to compare whether the two byte arrays contains the same content for each element?

For example, byte array {0x1, 0x2} is the same as {0x1, 0x2}. But byte array {0x1, 0x2} and byte array {0x2, 0x1} are not the same.

like image 345
George2 Avatar asked Sep 07 '09 14:09

George2


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

Well, you could use:

public static bool ByteArraysEqual(byte[] b1, byte[] b2)
{
    if (b1 == b2) return true;
    if (b1 == null || b2 == null) return false;
    if (b1.Length != b2.Length) return false;
    for (int i=0; i < b1.Length; i++)
    {
        if (b1[i] != b2[i]) return false;
    }
    return true;
}

(I normally use braces for everything, but I thought I'd experiment with this layout style just for a change...)

This has a few optimisations which SequenceEqual can't (or doesn't) perform - such as the up-front length check. Direct array access will also be a bit more efficient than using the enumerator.

Admittedly it's unlikely to make a significant difference in most cases...

You could possibly make it faster in unmanaged code by making it compare 32 or 64 bits at a time instead of 8 - but I wouldn't like to code that on the fly.

like image 153
Jon Skeet Avatar answered Sep 23 '22 03:09

Jon Skeet


You can use the SequenceEqual method:

bool areEqual = firstArray.SequenceEqual(secondArray);

As mentioned in the comments, SequenceEqual requires .NET 3.5 (or LINQBridge if you're using VS2008 and targeting an earlier version of the framework).

like image 21
LukeH Avatar answered Sep 23 '22 03:09

LukeH