1

I have an array of all the bytes in a file.

{ 0xA0, 0x00, 0xFF, 0xFF, 0xA0, 0x10, 0x01, 0x00, 0x10, 0x32, 0x0D, 0x00, 0x48, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0x6F, 0x00, 0x2C, 0x00, 0x20, 0x00, 0x77, 0x00, 0x6F, 0x00, 0x72, 0x00, 0x6C, 0x00, 0x64, 0x00, 0x21, 0x00, 0xF0, 0xF0 }

I also have a function that reads a certain section of the file, and moves the location farther forward. (Like BinaryReader.ReadX() except in an array.

    private static byte[] readBytes(int length)
    {
        cursorLocation += length;
        return new ArraySegment<byte>(bytes, cursorLocation-length, length).ToArray();
    }

However, when I try to test if a certain section of bytes are the same, it seems to not work. For example, this:

    byte[] startup = readBytes(4);

    if (startup == new byte[] { 0xA0, 0x00, 0xFF, 0xFF })
    {
        Console.WriteLine("Matches");
    } else
    {
        Console.WriteLine("Does not match.");
    }

returns "Does Not Match," instead of "Matches." I have also tried comparing each array piece in a row, but that also does not work. Printing them out in a line like so:

Console.WriteLine(startup[0]);
Console.WriteLine(startup[1]);
Console.WriteLine(startup[2]);
Console.WriteLine(startup[3]);

returns, as expected:

160
0
255
255

I am really not sure what is wrong, and any help would be greatly appreciated. Thanks so much!

3
  • 3
    == on arrays is a reference equality check, not a contents check. Commented May 1, 2020 at 18:07
  • +@MarcGravell Interesting, should I use === instead or something? Thanks Commented May 1, 2020 at 18:09
  • 1
    The === operator doesn't exist in C#. You have to write a custom method that compares the two arrays element by element. Of course you should also make sure first that they are of same length and not null. Commented May 1, 2020 at 18:57

1 Answer 1

1

In addition to my comment above, here is an implementation of a method that you could use to compare the arrays.

    private static bool AreByteArraysEqual(byte[] expected, byte[] actual)
    {
        return expected != null
            && actual != null
            && expected.Length == actual.Length 
            && Enumerable.Range(0, actual.Length - 1)
                         .All(i => expected[i] == actual[i]);
    }

Using == to compare the two arrays returns false in your case because this operator checks whether both arrays are the very same instance by comparing their reference as Marc Gravell already pointed out in his comment.

Update:

There was a comment to my answer that pointed out there is already a Linq method 'SequenceEqual' to compare collections element by element.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.