Convert IP2Location CSV data into IP ranges or CIDR

Intro

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 0.0.0.0 to 255.255.255.255 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 110.159.56.137 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 110.159.56.137 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

5

23

90

100

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

100

23

5

90

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 https://github.com/ip2location/ip2location-csv-converter which is a PHP script. This means you must have PHP installed if you want to use this script.

You can get PHP from https://www.php.net/manual/en/install.php 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:

“34989056”,”34989567″,”NL”,”Netherlands”
“34989568”,”34989823″,”US”,”United States”
“34989824”,”34990079″,”NL”,”Netherlands”
“34990080”,”34991103″,”ZA”,”South Africa”

Sample output:

“2.21.228.0”,”2.21.229.255″,”NL”,”Netherlands”
“2.21.230.0”,”2.21.230.255″,”US”,”United States”
“2.21.231.0”,”2.21.231.255″,”NL”,”Netherlands”
“2.21.232.0”,”2.21.235.255″,”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”
“34979840”,”34983935″,”DE”,”Germany”
“34983936”,”34988031″,”FR”,”France”
“34988032”,”34988799″,”ES”,”Spain”

Sample output:

“2.21.184.0/21″,”GB”,”United Kingdom”
“2.21.192.0/20″,”DE”,”Germany”
“2.21.208.0/20″,”FR”,”France”
“2.21.224.0/23″,”ES”,”Spain”

Was this article helpful?

Related Articles