title: Strange SNMP shifting operation
body: <p>The following is from the book C# Network Programmingm by Richard Blum:</p>
  
  <pre><code>public byte[] get(string request, string host, string community, string_ mibstring)
  {
      byte[] packet = new byte[1024];
      byte[] mib = new byte[1024];
      int snmplen;
      int comlen = community.Length;
      string[] mibvals = mibstring.Split('.');
      int miblen = mibvals.Length;
      int cnt = 0, temp, i;
      int orgmiblen = miblen;
      int pos = 0;
      // Convert the string MIB into a byte array of integer values
      // Unfortunately, values over 128 require multiple bytes
      // which also increases the MIB length
      for (i = 0; i &lt; orgmiblen; i++)
      {
          temp = Convert.ToInt16(mibvals[i]);
          if (temp &gt; 127)
          {
              mib[cnt] = Convert.ToByte(128 + (temp / 128));
              mib[cnt + 1] = Convert.ToByte(temp - ((temp / 128) * 128));
              cnt += 2;
              miblen++;
          } 
          else
          {
          mib[cnt] = Convert.ToByte(temp);
          cnt++;
          }
      }
      snmplen = 29 + comlen + miblen - 1; //Length of entire SNMP packet
      //The SNMP sequence start
      packet[pos++] = 0x30; //Sequence start
      packet[pos++] = Convert.ToByte(snmplen - 2); //sequence size
      //SNMP version
      packet[pos++] = 0x02; //Integer type
      packet[pos++] = 0x01; //length
      packet[pos++] = 0x00; //SNMP version 1
      //Community name
      packet[pos++] = 0x04; // String type
      packet[pos++] = Convert.ToByte(comlen); //length
      //Convert community name to byte array
      byte[] data = Encoding.ASCII.GetBytes(community);
      for (i = 0; i &lt; data.Length; i++)
      {
          packet[pos++] = data[i];
      }
  }
  </code></pre>
  
  <p>I didn't understand the following code:</p>
  
  <pre><code>    for (i = 0; i &lt; orgmiblen; i++)
      {
          temp = Convert.ToInt16(mibvals[i]);
          if (temp &gt; 127)
          {
              mib[cnt] = Convert.ToByte(128 + (temp / 128));
               mib[cnt + 1] = Convert.ToByte(temp - ((temp / 128) * 128));
               cnt += 2;
               miblen++;
          } 
          else
          {
              mib[cnt] = Convert.ToByte(temp);
              cnt++;
          }
      }
  </code></pre>
  
  <p>I do understand this is for putting down into two bytes if the temp is larger then one byte. But what is the calculation being done 128+(temp/128) and then for the second byte: temp- (temp/128)*128, this is what i don't understand.</p>
  
  <p>Please help, Thank you.</p>