Click here to Skip to main content
16,004,806 members
Articles / Web Development / HTML
Tip/Trick

IP and Subnet Calculator Using C#

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
26 Aug 2016CPOL 35.8K   2.3K   11   3
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAddition Request Pin
samerselo25-Sep-19 1:35
samerselo25-Sep-19 1:35 
QuestionNice Article Pin
Sneha Palve30-Aug-16 2:38
Sneha Palve30-Aug-16 2:38 
QuestionVote of 3 Pin
Daniel Jursza29-Aug-16 21:14
Daniel Jursza29-Aug-16 21:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.