Given the IP address and subnet mask, how do I calculate CIDR?

Ok, I can't figure it out: given the following:

IP address = 192.168.1.0
Subnetmask = 255.255.255.240

Using C #, how do I calculate CIDR 192.168.1.0/28 notation? Is there an easy way to achieve this? Am I missing something?

Thanks!

+4
source share
5 answers

I don't have C # code, but here is the answer in VB. It should not be 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 
+2
source

256 - 240 = 16 = 2**4, 32 - 4 = 28

This is not a C # question.

To get the network address from the IP address and mask, you can apply the default and to the IP and mask. You can get bytes from a string using IPAddress.Parse() and IPAddress.GetAddressBytes() .

+6
source

I had to do the same, no new information, but this snippet may come in handy for the next person who is looking for a way to do this in C #. note that this method only takes into account the number of consecutive 1s and leaves you the job of adding it to 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; } } 
+3
source

See Get CIDR from Network Mask

Using:

 var cidrnetmask = MaskToCIDR(IPAddress.Parse("255.0.0.0").GetAddressBytes()); 

This works for IPv4. To support IPv6, it was possible to increase the number of bytes, but hopefully no one will try to use the old-style network layouts for IPv6: o)

Method:

 static int MaskToCIDR(byte[] bytes) { var b0 = bytes[0]; var b1 = bytes[1]; var b2 = bytes[2]; var b3 = bytes[3]; return b3 != 0 ? ( (b3 & 0x01) != 0 ? 32 : (b3 & 0x02) != 0 ? 31 : (b3 & 0x04) != 0 ? 30 : (b3 & 0x08) != 0 ? 29 : (b3 & 0x10) != 0 ? 28 : (b3 & 0x20) != 0 ? 27 : (b3 & 0x40) != 0 ? 26 : 25 ) : b2 != 0 ? ( (b2 & 0x01) != 0 ? 24 : (b2 & 0x02) != 0 ? 23 : (b2 & 0x04) != 0 ? 22 : (b2 & 0x08) != 0 ? 21 : (b2 & 0x10) != 0 ? 20 : (b2 & 0x20) != 0 ? 19 : (b2 & 0x40) != 0 ? 18 : 17 ) : b1 != 0 ? ( (b1 & 0x01) != 0 ? 16 : (b1 & 0x02) != 0 ? 15 : (b1 & 0x04) != 0 ? 14 : (b1 & 0x08) != 0 ? 13 : (b1 & 0x10) != 0 ? 12 : (b1 & 0x20) != 0 ? 11 : (b1 & 0x40) != 0 ? 10 : 9 ) : b0 != 0 ? ( (b0 & 0x01) != 0 ? 8 : (b0 & 0x02) != 0 ? 7 : (b0 & 0x04) != 0 ? 6 : (b0 & 0x08) != 0 ? 5 : (b0 & 0x10) != 0 ? 4 : (b0 & 0x20) != 0 ? 3 : (b0 & 0x40) != 0 ? 2 : 1 ) : 0; } 
0
source

my solution, first analyze IPAddress:

 var Subnetmask = "255.255.255.240"; IPAddress ip = IPAddress.Parse(Subnetmask); 

then check the number of bits set in the ip mask:

 var intAddress = (int)IPAddress.Address; Console.WriteLine(NumberOfSetBits(intAddress)); //28 

function (from fooobar.com/questions/1304999 / ... ):

 int NumberOfSetBits(int i) { i = i - ((i >> 1) & 0x55555555); i = (i & 0x33333333) + ((i >> 2) & 0x33333333); return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; } 
0
source

Source: https://habr.com/ru/post/1304996/


All Articles