Posts
22
Comments
75
Trackbacks
0
Converting IP Address to its Decimal Equivalent

Last time I posted something about getting all the host between a given IP Range. Part of the routine is to convert the IpAddress to its DecimalEquivalent. 

Tip: Asside from using the URL of a web site, you can use the Decimal version of the Ip address of the Host Name. Sample
        Google.com --> 74.125.71.105
        you can use http://1249724265
 
Solution Version 1      
public abstract class IpMath
{
 public static Int64 ConvertToDecimal(string IpAddress)
        {
            Int64[] tmpIpAddress = SplitIpAddress(IpAddress);

            Int64 tmpReturn = (
               tmpIpAddress[0] << 24 |
               tmpIpAddress[1] << 16 |
               tmpIpAddress[2] << 8 |
               tmpIpAddress[3]);

            return tmpReturn;
        }


        public static Int64[] SplitIpAddress(string IpAddress)
        {
            Int64[] tmpReturn = new Int64[4]; ;
            Int64 counter = 0;
           
            foreach (string tmpString in IpAddress.ToString().Split(".".ToCharArray(), 4))
            {
                tmpReturn[counter] = Convert.ToInt64(tmpString);
                counter += 1;
            }

            return tmpReturn;
        }
}


Converting the Ip address to its decimal format is very simple, as listed above, ConvertToDecimal(string IpAddress). First you have to split the String version of the Ip address to 4 parts(first octet to fourth octet). This is the main purpose of the SplitIpAddress(string IpAddress) function as listed above. Next is we have to shift each part to its proper places as explained below using the << Operator, ....

 tmpIpAddress[0] << 24 // This simply means that we have to shift the binary digits 24 places to the left
 0xFF000000 or 11111111000000000000000000000000 in Binary
  
 
 tmpIpAddress[1] << 16 // This simply means that we have to shift the binary digits 16 places to the left
 0x00FF0000 or 00000000111111110000000000000000 in Binary


 tmpIpAddress[2] << 8 // This simply means that we have to shift the binary digits 8 places to the left
 0x0000FF00 or 00000000000000001111111100000000 in Binary


 0x000000FF is equal to 00000000000000000000000011111111 in Binary
 tmpIpAddress[3]);
 
After shifting the digits to their proper place, we need to combine all of them using the | Operator.

------------------------------------------------------------------------------

Solution Version 2
Asside from using the << operator we can also multiply the octets to  

  16777216 * (Int64)bytes[0] // 1000000000000000000000000
   65536 * (Int64)bytes[1]  // 10000000000000000 
   256 * (Int64)bytes[2]   // 100000000
  (Int64)bytes[3])  

and asside from creating our own Split Function, We can use System.Net.IPAddress.TryParse

final Function

public static Int64 IpToDecimalVersion3(string ipAddress)
        {
            System.Net.IPAddress ip;
            if (System.Net.IPAddress.TryParse(ipAddress, out ip))
            {
                byte[] bytes = ip.GetAddressBytes();
                return (Int64)(
                    16777216 * (Int64)bytes[0] +
                    65536 * (Int64)bytes[1] +
                    256 * (Int64)bytes[2] +
                    (Int64)bytes[3]);
            }
            else
                return 0;
        }

-----------------------------------------------------------------------------------------

Solution Version 3
We can also use the Modulo Operator % to threat the IP address as base 256 and convert them to decimal

 public double IpToDecimalVersion4(string IPaddress)
        {
            int counter;
            string[] SplittedIpAddress;
            double tmpReturn = 0;

            if (IPaddress == "")
            {
                return 0;
            }
            else
            {
                SplittedIpAddress = IPaddress.Split('.');
                for (counter = SplittedIpAddress.Length - 1; counter >= 0; counter--)
                {
                    tmpReturn += ((int.Parse(SplittedIpAddress[counter]) % 256) * Math.Pow(256, (3 - counter)));
                }
                return tmpReturn;
            }
        }

------------------------------------------------------------------------------------------

Solution Version 4
lastly we can use LINQ =), I found this somewhere else but I can't remember on to which forum.

        public static string IpToDecimalVersion2(string ipAddress)
        {   
            int shift = 3; 
            var octets = ipAddress.Split('.').Select(p => long.Parse(p));
            return octets.Aggregate(0L, (total, octet) => (total + (octet << (shift-- * 8)))).ToString();
        }

posted on Monday, January 24, 2011 12:11 PM Print
Comments
Gravatar
# re: Converting IP Address to its Decimal Equivalent
amin
10/25/2011 1:26 PM
ha to convert thid dite to ip addresss
Gravatar
# re: Converting IP Address to its Decimal Equivalent
Mike
1/25/2012 3:21 PM
Very useful info, thanks a lot.

Post Comment

Title *
Name *
Email
Comment *  
Verification
Tag Cloud