C# code - SNMP packet processing I have the following code, which does the following:

Reads from the packet from UDP (SNMP) into an array, check if this number is > 127, if yes then '&amp;' this with 127, now pad it with leading zero's to make it 7 bits in total. Combine it with result. 
Now check the next one in the packet and repeat the above. 
It works for if 3 numbers > 127. Can this code be optimized check for unlimited throughout the length of the array

Please do let me know if you require any further information.

Thank you.

 int[] obj = new int[Objectlength];
 string result = string.Empty;
 for (int cnt = 0; cnt &lt; Objectlength; cnt++)
 {
     obj[cnt] = Convert.ToInt16(packet[objectstart]);
     objectstart++;

     if (obj[cnt] &gt;127)
     {

         int brush = 127;
         int bin = obj[cnt] &amp; brush;
         string binR1 = Convert.ToString(bin, 2).PadLeft(7, '0');
         result = result + binR1;
         cnt++;

         if ((packet[objectstart] &gt; 127) | (packet[objectstart]  &lt; 127))
         {

             Console.WriteLine(packet[objectstart]);
             int bin1 = packet[objectstart] &amp; brush ;
             binR1 = Convert.ToString(bin1, 2).PadLeft(7, '0');
             result = result + binR1;

             obj[cnt] = Convert.ToInt16(packet[objectstart]);
             cnt++;
             objectstart++;

             if ((packet[objectstart] &gt; 127) | (packet[objectstart] &lt; 127))
             {

                 Console.WriteLine(packet[objectstart]);
                 int bin2 = packet[objectstart] &amp; brush;
                 binR1 = Convert.ToString(bin2, 2).PadLeft(7, '0');
                 result = result + binR1;

                 obj[cnt] = Convert.ToInt16(packet[objectstart]);
                 objectstart++;
             }

         }

     }

}
First things first.  Your variable names are not very well thought out.  Even after I decode it, cnt or count?? does not describe what it does.  I think a better name would be currentIndex.

Second, C# standards say to use camelCasing for variable names.  I would also read up on var, it is a very useful keyword for cleaning up code.

Third, brush should be a constant, if you follow my suggestion on the change to recursion, it should be made a class constant.

I also don't like that cnt is incremented outside of the loop.

Now onto the main question.

Just seeing if ((packet[objectstart] &gt; 127) | (packet[objectstart]  &lt; 127)) twice in the same structure (one inside the other one) is a huge code smell to me, especially when it is the same code inside the second, as it is the first.  This looks like a good case for recursion, a method calling itself.

It would go something like this:

private string ProcessSegment(int[] obj, int currentIndex, int objectStart, dynamic[] packet)
{
     if ((packet[objectstart] != 127) || currentIndex &gt; Objectlength)
     {
         return string.Empty;
     }

     Console.WriteLine(packet[objectstart]);

     var bin2 = packet[objectstart] &amp; brush;
     binR1 = Convert.ToString(bin2, 2).PadLeft(7, '0');

     obj[currentIndex] = Convert.ToInt16(packet[objectstart]);

     return binR1 + ProcessSegment(obj, currentIndex++, objectStart++, packet);
}


Your calling code will look something like:

obj[cnt] = Convert.ToInt16(packet[objectstart]);
objectstart++;

if (obj[cnt] &gt;127)
{
    result += ProcessSegment(obj, cnt++, objectstart++, packet);
}


I have no way of testing this, but I think it should work.  Also note, please change the dynamic in the ProcessSegment call to be whatever the packet variable is