After reading this post by mrhinkydink I thought I’d share a c# way to accomplish the same thing; converting an IP address between dotted decimal notation and its numeric format. There are pre-written libraries and name spaces available everywhere to accomplish this task, but I’m going to show you the raw maths of the conversion process.
First, let’s construct a method to accept a string representation of an ipv4 address in dotted decimal notation (that’s 127.0.0.1) and convert it into numeric, or “long” format (2,130,706,433):
1
2
3
4
5
6
7
8
9
public Double IpStringToLong( String ipString )
{
var octets = ipString.Split('.');
return Double.Parse( octets[3] )
+ Double.Parse( octets[2] ) * 256
+ Double.Parse( octets[1] ) * 65536
+ Double.Parse( octets[0] ) * 16777216;
}
Okay, and back the other way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public string LongToString(Double ipAsLong)
{
var ip = String.Empty;
for (var n = 1; n < 5; n++)
{
var octet = Convert.ToInt32(Math.Truncate(ipAsLong / Math.Pow(256, 4 - n)));
ipAsLong = ipAsLong - (octet * Math.Pow(256, 4 - n));
if (octet > 255) return "0.0.0.0";
if (n == 1)
{
ip = octet.ToString(CultureInfo.InvariantCulture);
}
else
{
ip += "." + octet;
}
}
return ip;
}
Finally if the input is either a string- or a long and we don’t know which but we want the opposite:
1
2
3
4
5
6
7
8
var value = "127.0.0.1";
var value = 2130706433;
if (value.IndexOf(".") != -1)
{
return String.Format("{0} in decimal is {1}", value, StringToLong(value));
}
return String.Format("{0} in dotted decimal is {1}", value, LongToString(value));
Adding extension method class to this post:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Net;
namespace Program
{
public static class IPAddressExtensions
{
public static uint ToInt(this IPAddress ip)
{
var octets = ip.ToString().Split('.');
return uint.Parse(octets[3]) +
uint.Parse(octets[2]) * 256 +
uint.Parse(octets[1]) * 65536 +
uint.Parse(octets[0]) * 16777216;
}
public static IPAddress ToIpAddress(this uint value)
{
return ToIpAddress((double)value);
}
public static IPAddress ToIpAddress(this double value)
{
var ip = string.Empty;
for (var n = 1; n < 5; n++)
{
var octet = Math.Truncate(value / Math.Pow(256, 4 - n));
value = value - octet * Math.Pow(256, 4 - n);
if (octet > 255) return IPAddress.Parse("0.0.0.0");
if (n == 1)
{
ip = octet.ToString();
}
else
{
ip += "." + octet;
}
}
return IPAddress.Parse(ip);
}
}
}