3

I have byte buffer:

byte[] buffer = new byte[3];
List<byte[]> list;

Now I am adding:

 while ((count = reader.Read(buffer, 0, buffer.Length)) != 0)
 {       
      bool contains = l.Contains<byte[]>(buffer); //This is not working and checking only reference 

      if (!contains)                          
      {                          
        l.Add(new byte[] buffer[0],buffer[1],buffer[2]});              
      }                
  }

How to check if list contains byte array wchich has the same values as buffer?

1
  • Please add a language tag to your question. (I am assuming C# but I am not sure) Commented Sep 25, 2013 at 11:22

2 Answers 2

8

Your current version is not working because it does a check based on reference equality.

You want to find out if any list elements contain the same sequence of bytes:

bool contains = list.Any(x => x.SequenceEqual(buffer));
Sign up to request clarification or add additional context in comments.

Comments

0
public static bool ContainsSequence(byte[] toSearch, byte[] toFind) {
  for (var i = 0; i + toFind.Length < toSearch.Length; i++) {
    var allSame = true;
    for (var j = 0; j < toFind.Length; j++) {
      if (toSearch[i + j] != toFind[j]) {
        allSame = false;
        break;
      }
    }

    if (allSame) {
      return true;
    }
  }

  return false;
}

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.