65.9K
CodeProject is changing. Read more.
Home

IP and Subnet Calculator Using C#

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (6 votes)

Aug 26, 2016

CPOL
viewsIcon

35963

downloadIcon

2337

IP Calculator And Subnet

Introduction

This article will help you to calculate subnet mask base on NetMask and also BroadCast and Network Address

Background

The Subnet is important part in this Topic BroadCast and Network address was already present before.Based on the Netmask it will calculate the Subnet Mask and display the result.

 

Subnet Mask Calculation

Based on your Netmask as input it will provide the subnet mask

//This Function is Used to get Subnet basede on NetMask(i.e 0-32)
   public string getSubnetAddressFromIPNetMask(string netMask)
        {
            string subNetMask = string.Empty;
            int calSubNet = 0;
            double result = 0;
            if (!string.IsNullOrEmpty(netMask))
            {
                calSubNet = 32 - Convert.ToInt32(netMask);
                if (calSubNet >= 0 && calSubNet <= 8)
                {
                    for (int ipower = 0; ipower < calSubNet; ipower++)
                    {
                        result += Math.Pow(2, ipower);
                    }
                    double finalSubnet = 255 - result;
                    subNetMask = "255.255.255." + Convert.ToString(finalSubnet);
                }
                else if (calSubNet >8 && calSubNet<=16)
                {
                    int secOctet = 16 - calSubNet;
                   
                        secOctet = 8-secOctet ;
                   
                    for (int ipower = 0; ipower < secOctet; ipower++)
                    {
                        result += Math.Pow(2, ipower);
                    }
                    double finalSubnet = 255 - result;
                    subNetMask = "255.255." + Convert.ToString(finalSubnet)+".0";
                }
                else if (calSubNet > 16 && calSubNet <= 24)
                {
                    int thirdOctet = 24 - calSubNet;
                   
                        thirdOctet = 8-thirdOctet ;
                 
                    for (int ipower = 0; ipower < thirdOctet; ipower++)
                    {
                        result += Math.Pow(2, ipower);
                    }
                    double finalSubnet = 255 - result;
                    subNetMask = "255." + Convert.ToString(finalSubnet) + ".0.0";
                }
                else if (calSubNet > 24 && calSubNet <= 32)
                {
                    int fourthOctet = 32 - calSubNet;
                  
                        fourthOctet = 8-fourthOctet ;
                
                    for (int ipower = 0; ipower < fourthOctet; ipower++)
                    {
                        result += Math.Pow(2, ipower);
                    }
                    double finalSubnet = 255 - result;
                    subNetMask =  Convert.ToString(finalSubnet) + ".0.0.0";
                }
            }
            
            return subNetMask;
        }

 

History

Code basline