checking sequence of numbers through byte array in c#

ghj Grandhe
ghj Grandhe used Ask the Experts™
on
Actually i need to check the sequence of elements in an byte array in c#
where here "d" and "q" are character arrays.

here the below 2 lines are converting it into byte[]
iam getting error at    "if((item)==p.Contains(Convert.ToInt32(item)+1))"
byte[] p = Encoding.UTF8.GetBytes(d);
            byte[] r = Encoding.UTF8.GetBytes(q);
         
            foreach (var item in p)
            {
                if((item)==p.Contains(Convert.ToInt32(item)+1))
                {
                    count1 += 1;
                }

could you please help the error is byte[] doesnot contain 'contains' .
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
I see two methods:
1) Use List<byte>  instead of array.
2) Use IndexOf
int pos = p.IndexOf("yourValue");
if (pos > -1)
{
  //do something
}

Open in new window

Author

Commented:
i converted it into list and i can able to get some clues

  List<byte> p = Encoding.UTF8.GetBytes(d).ToList();
            List<byte> r = Encoding.UTF8.GetBytes(q).ToList();
         
            foreach (var item in p)
            {
                if(Convert.ToBoolean(item)==p.Contains(Convert.ToByte((item)+1)))
                {
//operation
}
}
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
You can do this very simply with LINQ.

If you just want to increase the count based on the number of matches:

var p = Encoding.UTF8.GetBytes(d).ToList();
count += p.Count(item => p.Contains(++item));

Open in new window

If you want to loop over just the matches:

var p = Encoding.UTF8.GetBytes(d).ToList();
foreach (var item in p.Where(item => p.Contains(++item)))
{
    Console.WriteLine($"Match: {item}");
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial