How to convert CIDR to network and IP address range in C#? -
To convert a network to CIDR notation (72.20.10.0/24) to find some C # code I Looking at something. ) For an IP address range, without luck. There are some threads about CIDR on Stackoverlow, but there is no C # code and I need the same which I need. So I decided to cook it myself, and I system for any conversion in this version. Did not want to trust the net.
Maybe it could be of help to anyone
Reference:
"whatmask" to c code
Usage:
UIP startIP, andIP; Network2IpRange ("72.20.10.0/24", Outout Out, OutIP Out);
The code considers 32 bit for everything
Fixed zero network 2 IPRange (string senetwork, UIT Startup, uit and ip out) {UIT IP, / * IP address * / mask, / * subnet mask * / broadcast, / * broadcast address * / network; / * Network address * / int bits; String [] element = sNetwork.Split (new four [] {'/'}); IP = IP2 element (element [0]); Bit = convert toint32 (element [1]); Mask = ~ (0xffffffff & gt; bits); Network = IP & amp; Mask; Broadcast = network + ~ mask; UsableIps = (bits> 30)? 0: (broadcast network - 1); If (usableIps & lt; = 0) {startIP = endIP = 0; } Other {startIP = Network + 1; EndIP = broadcast - 1; }} Public Fixed UIT IP2Int (String IPNumber) {UIT IP = 0; String [] element = IPNumber.Split (new four [] {'.'}}; If (elements. Length == 4) {ip = convert toUInt32 (element [0])
Feel free to submit your correction.
I recommend using the C # IP network class.
string net =" 192.168.168.100/24 "; IPNetwork ipnetwork = IPNetwork.Parse (n Console.lightline ("Network: {0}", ipnetwork.Network); console.light line ("netmask: {0}", ipnetwork.Netmask); console.light line ("broadcast: {0}", Ipnetwork.broadcast; console.light line ("first use: {0}", ipnetwork.FirstUsable); console.light line ("end use: {0}", ipnetwork.LastUsable); console.light line ("use Qualified: {0} ", ipnetwork.Usable); console.light line (" cedar: {0} ", ipnwork.cder);
this will output
Network: 1920.168.168.0 Netmask: 255.255.255.0 Broadcast: 192.168.168.255 FAR T Ughebl: 192.168.168.1 last Ughebl: 192.168.168.254 usable: 254 Cedar: 24
Whether funny.
Comments
Post a Comment