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, 18.104.22.168/24, which represent the IP ranges from 22.214.171.124 to 126.96.36.199. 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 188.8.131.52, or IP number, for example, 3401190660, as the input parameters. The function will convert them into IP number if dotted IP address format were 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 contain the list of CIDR formatted IP address. Looping have 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.