How to convert CIDR IP Range into range of IPv4 Addresses using JavaScript?

A lot of times we need to understand the IP Addresses of the customers visiting the website. If you have a specific IP Address given to you, all is fine. But if you have a CIDR format of IP Range, things get fuzzy. CIDR format is a shortcut to write a range of IP Addresses. Example CIDR IP Range looks like “110.40.240.16/22” which covers all the IP Addresses starting from “110.40.240.0”  to “110.40.243.255”. We have a ton of CIDR to IPv4 converters available on the web which can do this conversion. But if you are developing an application which needs this converter in it, this post explains exactly that.

You can read more about CIDR Notation and other details here. In this post, let us just concentrate on writing JavaScript code which can take CIDR as an input and give us the starting, ending IP Addresses and the total number of IPs into an array. This function can be used either in the client side or server side (if you are using NodeJS to build your application).

JavaScript Code to convert CIDR to IPv4

Explanation of Code

We are calling the function “cidrtoip” where it takes CIDR notation in string format as the parameter. Eg: cidrtoip(“110.40.240.16/22”). The first line of the code splits this CIDR string into two parts where the second part denotes the number of subnet bits and the first part is used in the next lines. To get the number of host bits, you need to subtract the subnet bits from 32 because your IPv4 address is 32 bit. Before converting this into the Binary format, we are going to split the first part further into four parts.

We are using JavaScript’s map function to iterate over the array and modify each of these parts into binary format and then adding sufficient zeros to make it into an 8 digit binary code and then join all of them to form a long binary code. Next, we need to find out the starting IP and the ending IP address in the range. For this we need to make all the last “Number of Host Bits” digits (Eg: Here there are 10 host bits, so you need to change all the last 10 digits) to ZERO to get Starting IP which is also called Network ID and change all these 10 digits to ONE to get Broadcast ID or ending IP. Once we modify this into needed binary form, we need to split this again into four parts so that we can convert this back to normal IP addresses. So, we will use the match function to make these Binary format IPs into arrays of four elements.

Again we will use the map function to convert back these binary numbers to integers and attach them back using join function with a dot delimiter. Also, the total number of IPs that are possible in a given CIDR format is calculated as 2 to the Power(Host Bits). We are going to return these three things as an array in the last line.

P.S: I will be writing anything related to Digital Analytics and Tag Management on my old blog, Only Web Analytics, and this blog will have a much wider category of content sans the core stuff I used to write on Digital Analytics and Tag Management.

Ram Manohar

Author: Ram Manohar

Independent Digital Analytics and Tag Management Consultant. Web Analytics Expert with 8+ years of experience. Well versed in Digital Analytics, Web Programming, WordPress and Digital marketing. Contact me for consulting!