310
//A CIDR IP address looks like a normal IP address except that it ends with a slash followed by a number, called the IP network prefix.
1
using System;
2
using System.Diagnostics;
3
//https://docs.microsoft.com/en-us/archive/blogs/knom/ip-address-calculations-with-c-subnetmasks-networks
4
public static class SubnetMask
5
{
6
public static readonly System.Net.IPAddress ClassA = System.Net.IPAddress.Parse("255.0.0.0");
7
public static readonly System.Net.IPAddress ClassB = System.Net.IPAddress.Parse("255.255.0.0");
8
public static readonly System.Net.IPAddress ClassC = System.Net.IPAddress.Parse("255.255.255.0");
9
10
public static System.Net.IPAddress CreateByHostBitLength(int hostpartLength)
11
{
12
int hostPartLength = hostpartLength;
13
int netPartLength = 32 - hostPartLength;
14
15
if (netPartLength < 2)
16
throw new ArgumentException("Number of hosts is to large for IPv4");
17
18
Byte[] binaryMask = new byte[4];
19
20
for (int i = 0; i < 4; i++)
21
{
22
if (i * 8 + 8 <= netPartLength)
23
binaryMask[i] = (byte)255;
24
else if (i * 8 > netPartLength)
Cached Result