Given an IP address and subnetmask, how do I calculate the CIDR?
Solution 1
I don't have it as C# code but here is the answer in VB. Should not be to hard to convert.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim someIP As Net.IPAddress = Net.IPAddress.Parse("192.168.1.10")
Dim someMASK As Net.IPAddress = Net.IPAddress.Parse("255.255.255.240")
Dim ipL As Long = IPtoLong(someIP)
Dim maskL As Long = IPtoLong(someMASK)
'Convert Mask to CIDR(1-30)
Dim oneBit As Long = &H80000000L
Dim CIDR As Integer = 0
For x As Integer = 31 To 0 Step -1
If (maskL And oneBit) = oneBit Then CIDR += 1 Else Exit For
oneBit = oneBit >> 1
Next
Dim answer As String = LongToIp(ipL And maskL) & " /" & CIDR.ToString
End Sub
Public Function IPtoLong(ByVal theIP As Net.IPAddress) As Long 'convert IP to number
Dim IPb() As Byte = theIP.GetAddressBytes 'get the octets
Dim addr As Long 'accumulator for address
For x As Integer = 0 To 3
addr = addr Or (CLng(IPb(x)) << (3 - x) * 8)
Next
Return addr
End Function
Public Function LongToIp(ByVal theIP As Long) As String 'convert number back to IP
Dim IPb(3) As Byte '4 octets
Dim addr As String = "" 'accumulator for address
Dim mask8 As Long = MaskFromCidr(8) 'create eight bit mask
For x = 0 To 3 'get the octets
IPb(x) = CByte((theIP And mask8) >> ((3 - x) * 8))
mask8 = mask8 >> 8
addr &= IPb(x).ToString & "." 'add current octet to string
Next
Return addr.TrimEnd("."c)
End Function
Private Function MaskFromCidr(ByVal CIDR As Integer) As Long
MaskFromCidr = CLng(2 ^ ((32 - CIDR)) - 1) Xor 4294967295L
End Function
Solution 2
256 - 240 = 16 = 2**4, 32 - 4 = 28
It is not really a C# question.
To get a net address from an IP and mask you can apply bytewise and
to the IP and mask. You can get bytes from a string using IPAddress.Parse()
and IPAddress.GetAddressBytes()
.
Solution 3
I had to do the same thing, no new info but this snippet may come in handy for the next person looking for a way to do this in C#. note that this method only counts the number of consecutive 1s, and leaves you the work of appending it to the IP.
public class IPAddressHelper
{
public static UInt32 SubnetToCIDR(string subnetStr)
{
IPAddress subnetAddress = IPAddress.Parse(subnetStr);
byte[] ipParts = subnetAddress.GetAddressBytes();
UInt32 subnet = 16777216 * Convert.ToUInt32(ipParts[0]) + 65536 * Convert.ToUInt32(ipParts[1]) + 256 * Convert.ToUInt32(ipParts[2]) + Convert.ToUInt32(ipParts[3]);
UInt32 mask = 0x80000000;
UInt32 subnetConsecutiveOnes = 0;
for (int i = 0; i < 32; i++)
{
if (!(mask & subnet).Equals(mask)) break;
subnetConsecutiveOnes++;
mask = mask >> 1;
}
return subnetConsecutiveOnes;
}
}
Solution 4
Keep it simple!
This works for IPv4 only, but since IPv6 does only support CIDR like /64
in fe80::1ff:fe23:4567:890a/64
calculations like that are unnecessary there.
All you need for an IPv4 network mask:
int cidr = Convert.ToString(mask.Address, 2).Count( o => o == '1');
Explanation based on the given example:
IPAddress mask = new IPAddress(new byte[] { 255, 255, 255, 240 });
// maskBinAsString = 11110000111101001111111111111111
string maskBinAsString = Convert.ToString(mask.Address, 2);
// cidr = 28
int cidr = Convert.ToString(mask.Address, 2).Count( o=> o == '1');
RoelF
TPM with deep interest in Cloud, iot and Continuous Delivery.
Updated on July 17, 2022Comments
-
RoelF almost 2 years
Ok I can't seem to figure this out: given the following:
IP address = 192.168.1.0
Subnetmask = 255.255.255.240
Using c#, how do I calculate the CIDR notation
192.168.1.0/28
? Is there an easy way to achieve this? Am I missing something?Thanks!