Posts
22
75
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
you can use http://1249724265

Solution Version 1
public abstract class IpMath
{
{

Int64 tmpReturn = (

return tmpReturn;
}

{
Int64[] tmpReturn = new Int64; ;
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 << 24 // This simply means that we have to shift the binary digits 24 places to the left
0xFF000000 or 11111111000000000000000000000000 in Binary

tmpIpAddress << 16 // This simply means that we have to shift the binary digits 16 places to the left
0x00FF0000 or 00000000111111110000000000000000 in Binary

tmpIpAddress << 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

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 // 1000000000000000000000000
65536 * (Int64)bytes  // 10000000000000000
256 * (Int64)bytes   // 100000000
(Int64)bytes)

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

final Function

{
{
return (Int64)(
16777216 * (Int64)bytes +
65536 * (Int64)bytes +
256 * (Int64)bytes +
(Int64)bytes);
}
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

{
int counter;
double tmpReturn = 0;

{
return 0;
}
else
{
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.

{
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 # re: Converting IP Address to its Decimal Equivalent
amin
10/25/2011 1:26 PM
ha to convert thid dite to ip addresss # re: Converting IP Address to its Decimal Equivalent
Mike
1/25/2012 3:21 PM
Very useful info, thanks a lot.

Title *
Name *
Email
Comment *
Verification
Tag Cloud