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

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

functioncidrtoip(cidr){

variprange=cidr.split('/')[0];

varsubnetbits=cidr.split('/')[1];

varnumHostBits=32-parseInt(subnetbits);

varipParts=iprange.split('.');

varipPartsBinary=ipParts.map(function(x){

varr=parseInt(x,10).toString(2);//Converting the number to Binary

varl=r.length;

//Adding sufficient Zeros to get 8 digit binary format

for(vari=0;i<(8-l);i++){

r="0"+r;

}

returnr;

});

varfullBinaryIp=ipPartsBinary.join('');

varnetworkIdBinary="";

varbroadcastIdBinary="";

for(vari=0;i<32;i++){

if(i<subnetbits){

networkIdBinary+=fullBinaryIp.charAt(i);

broadcastIdBinary+=fullBinaryIp.charAt(i);

}else{

networkIdBinary+="0";

broadcastIdBinary+="1"

}

}

networkIdParts=networkIdBinary.match(/.{1,8}/g);

broadcastIdParts=broadcastIdBinary.match(/.{1,8}/g);

varnetworkId=networkIdParts.map(function(x){

returnparseInt(x,2);

});

networkId=networkId.join('.');

varbroadcastId=broadcastIdParts.map(function(x){

returnparseInt(x,2);

});

broadcastId=broadcastId.join('.');

varnumIPs=Math.pow(2,numHostBits);

return[networkId,broadcastId,numIPs];

}

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.

Your email address will not be published. Required fields are marked *

Comment

Name *

Email *

Website

Notify me of follow-up comments by email.

Notify me of new posts by email.

Join our Readers

Never miss another great post. Receive free updates on your email!

About Me!

Hey Folks! I am Ram Manohar, author of this website. I am an Independent Digital Analytics and Tag Management Consultant with more than 8 years of experience consulting 15+ Enterprise clients and many small and medium businesses. You can know more about me and connect using below links!