This function takes in the CIDR address (/x notation) and returns it in a decimal notation (a.b.c.d)

If you’re looking for information about how CIDR works I recommend the wikipedia articles :
– http://en.wikipedia.org/wiki/Subnetwork
– http://en.wikipedia.org/wiki/CIDR_notation
– http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing

public string CIDR2DECIMAL(int cidr)
{
  string[] decim = new string[4];

  // We go through each octagon in the decimal address
  for (int i = 0; i < 4; i++)
  {
    if (cidr > 8)
    {
      decim[i] = "255";
      cidr -= 8;
    }
    else
    {
      int temp = 0;
      for (int a = 7; cidr > 0; a--, cidr--)
      {
        temp += (int)Math.Pow(2, a);
      }
      decim[i] = temp.ToString();
    }
  }
  return decim[0] + "." + decim[1] + "." + decim[2] + "." + decim[3];
}
Tagged with: