Convert IP2Location CSV data into IP ranges or CIDR


Anyone who is using the IP2Location CSV data will know that we use 2 fields called IPFrom and IPTo to denote the start and end of a range of IP addresses. In these 2 fields, we use what’s known as an IP number instead of an IP address.

What is an IP number?

It is actually very straightforward. IP numbers range from 0 to 4294967295. The equivalent in IP address form is to which covers all the IP addresses in IPv4.

To demonstrate how to convert IP number to IP address, we will convert IP number 1855928457 into which is the IP address form.

  • First, take 1855928457 and convert into binary or base-2 number. You’ll get 1101110100111110011100010001001.
  • Pad the number with zeroes on the left until you have 32 digits which is 01101110100111110011100010001001.
  • Now take the binary number and split into segments of 8 digits. You should get something like 01101110 10011111 00111000 10001001.
  • Now convert each segment back to decimal. You should get 110 159 56 137 which you can link using a dot to give you the IPv4 address of and that’s it.

TIP: If you aren’t sure how to convert decimal to binary and vice-versa, the Windows Calculator program has a Programmer mode which allows you easily convert both types of numbers.

Why use an IP number instead of IP address?

We have chosen to go with IP number because computer programs sort numbers easier than text. It also makes searching for a particular IP address range that much more efficient.

If you perform sorting as number, you’ll get something like below





When sorting as text, you’ll get a different result like below





As you can well see, text sorting does not make sense when you’re trying to locate a specific number in the records.

Ability to convert from IP number to IP address

While we have optimized our data using IP number, we do recognize that our users may operate various programs that would have trouble working with IP numbers. This is why we have provided the IP2Location CSV Converter script to allow our users to easily recreate our CSV data but using IP address ranges or even Classless Inter-Domain Routing (CIDR) form.

You can download the script from which is a PHP script. This means you must have PHP installed if you want to use this script.

You can get PHP from if you need it.

The usage of the script is very simple. Below are the 2 ways you can convert the CSV file.

To convert into IP address ranges, run the following command (modify example for your own BIN file names):

php ip2location-csv-converter.php -range IP2LOCATION-DB1.CSV IP2LOCATION-DB1.NEW.CSV

Sample input:

“34989568”,”34989823″,”US”,”United States”
“34990080”,”34991103″,”ZA”,”South Africa”

Sample output:

“”,”″,”US”,”United States”
“”,”″,”ZA”,”South Africa”

To convert into CIDR, run the following command:

php ip2location-csv-converter.php -cidr IP2LOCATION-DB1.CSV IP2LOCATION-DB1.NEW.CSV

Sample input:

“34977792”,”34979839″,”GB”,”United Kingdom”

Sample output:

“″,”GB”,”United Kingdom”

Was this article helpful?

Related Articles