FREE IP to Country Database Help

Help topics:

1. Database description »
2. Converting dot-decimal IP address notation to 32-bit decimal form »
3. Converting 32-bit decimal IP address notation to dot-decimal IP address notation »

 

Database description

We offer the database in CSV format. It has 4 fields namely 'startip', 'endip', 'countrycode', and 'countryname'. Below is the description of each of the fields:

'startip'

Represents the starting IPv4 address in 32-bit decimal form.

'endip'

Represents the ending IPv4 address in 32-bit decimal form.

'countrycode'  

ISO 3166-1 alpha-2 country code

'countryname'   

Country name

 

 

Converting dot-decimal IP address notation to 32-bit decimal form

Let's assume that your IP address is : 12.34.56.78

   The 32-bit decimal form value:   
      = (16777216 x 12) + (65536 x 34) + (256 x 56) + (78)   
      = 203569230  

   OR

   The 32-bit decimal form value:   
      = ( 12 <<24) + ( 34 <<16) + ( 56 <<8) + 78   
      = 203569230   

   Therefore the 32-bit decimal value =  203569230   


Note: The '<<' symbols means shift left.

 

Below is a sample function in PHP that will do the convertion

<?php

function convertIPtoInteger($ip)
{
	$ipArr = explode('.',$ip);
	return ($ipArr[0]<<24) + ($ipArr[1]<<16) + ($ipArr[2]<<8) + $ipArr[3];
}

?>

 

 

Converting 32-bit decimal IP address notation to dot-decimal IP address notation

Let's assume that your 32-bit decimal IP address: 203569230

   The dot-decimal IP address value: A.B.C.D   

   A = ( 203569230 / 16777216 ) % 256  = 12   
   B = ( 203569230 / 65536 ) % 256     = 34   
   C = ( 203569230 / 256 ) % 256       = 56   
   D = ( 203569230 ) % 256             = 78   

OR
   A = ( 203569230 & 0xFF000000 ) >> 24 = 12   
   B = ( 203569230 & 0xFF0000   ) >> 16 = 34   
   C = ( 203569230 & 0xFF00     ) >> 8  = 56   
   D = ( 203569230 & 0xFF       )       = 78   

   Therefore the IP address is : 12.34.56.78   


Note: The '>>' symbols means shift right while
      the '%' means modulo or remainder of division.

 

Below is a sample function in PHP that will do the convertion

<?php

function convertIntegertoIP($num)
{
	$ipVal = $num;
	$ipArr = array(0 => floor($ipVal/0x1000000));
	$ipVint   = $ipVal-($ipArr[0]*0x1000000); // for clarity
	$ipArr[1] = ($ipVint & 0xFF0000)  >> 16;
	$ipArr[2] = ($ipVint & 0xFF00  )  >> 8;
	$ipArr[3] =  $ipVint & 0xFF;
	return implode('.', $ipArr);
}

?>