简体   繁体   中英

Convert IP addresses range to CIDR format and vice versa

I need to convert IP range to CIDR notation and vice versa.I solved that issue by IPNetwork library by the following way :

Conversion from CIDR to range :

    var ip = IPNetwork.Parse("192.168.168.100/24");
    Console.WriteLine(ip.FirstUsable.ToString() + "/" + ip.LastUsable.ToString());

And convertion from range to CIDR :

    IPNetwork network = IPNetwork.Parse("192.168.168.0");
    IPNetwork network2 = IPNetwork.Parse("192.168.168.255"); 
    IPNetwork ipnetwork = IPNetwork.Supernet(network, network2);
    Console.WriteLine(ipnetwork.FirstUsable + "/" + ipnetwork.Cidr);

It seems it could work fine. But when i use custom tools they return a little bit different result.

For 192.168.168.100/24

The IPNetwork lib returns 192.168.168.1 - 192.168.168.254

http://www.ipaddressguide.com/cidr returns 192.168.168.0 - 192.168.168.255

http://ipconvertertools.com/cidr2ipranges returns 192.168.168.1 - 192.168.168.254

http://ip2cidr.com/bulk-ip-to-cidr-converter.php returns

192.168.168.1/32
192.168.168.2/31
192.168.168.4/30
192.168.168.8/29
192.168.168.16/28

when i try to convert 192.168.168.1 - 192.168.168.254 to CIDR . I'm really confused that different tools return different result. Also, my goal is creating of white list for users and check their IP s when they come, so it is a very important issue.

Given the CIDR notation of 192.168.168.100/24:

Network address is 192.168.168.0 
First usable address is 192.168.168.1
Last usable address is 192.168.168.254 
Broadcast address is 192.168.168.255 
Network mask is 255.255.255.0

There is no and cannot be no ambiguity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM