Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

IP and Subnet Calculator Using C#

0.00/5 (No votes)
26 Aug 2016 1  
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

C#
//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 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here