title: How can I increment an IP address by a specified amount?
body: <p>I am trying to figure out how to increment a starting ip address, and increment it by an offset that I specify. I have attempted to do this, but I am doing something wrong because I am getting IPs that are all over the place, not even in the same network range.</p>
  
  <p>What I am currently doing is taking my starting ip and ending ip, getting the total amount of addresses then incrementing the total ips by an offset then attempting to actually increment the IP.</p>
  
  <p>I am incrementing to the total ips by an offset so I know how many to increment the ip. (I am completing different tasks per offset.) Whatever the loop has incremented "t" to that is how many I increment IPs. Now that I have given the rundown, my issue only seems to be with actually incrementing ips, can anyone help me out in this situation. Thanks.</p>
  
  <pre><code>            string from = txtStart.Text, to = txtEnd.Text;
              uint current = from.ToUInt(), last = to.ToUInt();
  
              ulong total = last - current;
              int offset = 3; //This is an example number, it actually could be anything.
  
              while (current &lt;= last)
              {
               for (int t = 0; t &lt; total; t += offset)
                      {
                          uint ut = Convert.ToUInt32(t);
                          current = current + ut;
                          var ip = current.ToIPAddress();
                      }  
                }
  </code></pre>
  
  <p>Here is the extension class I am using. They work fine.</p>
  
  <pre><code>public static class Extensions
      {
          public static uint ToUInt(this string ipAddress)
          {
              var ip = IPAddress.Parse(ipAddress);
              var bytes = ip.GetAddressBytes();
              Array.Reverse(bytes);
              return BitConverter.ToUInt32(bytes, 0);
          }
  
          public static string ToString(this uint ipInt)
          {
              return ToIPAddress(ipInt).ToString();
          }
  
          public static IPAddress ToIPAddress(this uint ipInt)
          {
              var bytes = BitConverter.GetBytes(ipInt);
              Array.Reverse(bytes);
              return new IPAddress(bytes);
          }
      }
  </code></pre>