byte[] does not equal a matching byte[], but the hex values match.

I have two byte arrays, one is 4 bytes long containing a known sequence, the other is file data. I am attempting to locate the 4-byte sequence in the file data by checking every four bytes in the file data, and using indexing to get the four bytes.

Here's the code:

byte[] data = File.ReadAllBytes(fileName);
byte[] header = new byte[] { 0x4B, 0x54, 0x53, 0x52 };
int pos = 0;
for (int i = 4; i < data.Length; i += 4) {
#if DEBUG
    Debug.WriteLine(Convert.ToHexString(data[pos..i]));
    bool isTrue = Convert.ToHexString(data[pos..i]) == Convert.ToHexString(header); // this is true
    bool isFalse = data[pos..i] == header; // this is false
#endif
    if (data[pos..i] == header) {
        break;
    }
    pos += 4;
}


Can anyone explain this behavior? And suggest how I can fix it?

Thanks
Was this page helpful?