In this tutorial, we will demonstrate you on how to convert IP address ranges into CIDR format. CIDR format, Classless Inter-Domain Routing, is a compact representation of an IP address and its associated routing prefix. An example of CIDR notation, 188.8.131.52/24, which represents the IP ranges from 184.108.40.206 to 220.127.116.11. Please take note that this tutorial only for the conversion of IPv4 to CIDR format.
This tutorial will not display you a complete project, but the core function of how the conversion work. This function, iprange2cidr, takes 2 input parameters, namely the ipStart and ipEnd, the starting and the ending of an IP address respectively. You can either supply the IP address in dotted format, for example, 18.104.22.168, or IP number, for example, 3401190660, as the input parameters. The function will convert them into IP number if the dotted IP address format was supplied and perform the calculation.
Below are the sample codes of the function written in several programming languages, such as PHP, VB.NET, C#, Java and Ruby.
The result that return by the iprange2cidr function is an Array that contains the list of CIDR formatted IP address. Looping has to be done to get all the CIDR formatted IP address from the return result. The following figure shows the sample output with ipStart:192.168.1.0 and ipEnd:192.168.1.9 of the converting IP address ranges into CIDR format.