Translate IP address from dotted-decimal address to decimal format

IP geolocation database

IP to Country Database
IP Region Database
IP to City Database
IP-Country-ISP Database
IP-Country-Region-City-ISP

Free tools

Target Country by IP Address
ISO 3166 Country Codes
Flag images of the World


IP geolocation databaseHome
Online OrderPurchase
Download filesDownloads

Target Country by IP Address IP-Country mapping Database

How do I convert a IP Address to a IP Number?

IP address (IPV4) is divided into 4 sub-blocks. Each sub-block has a different weight number each powered by 256. IP number is being used in the database because it is efficient to search between a range of number in database.

Start IP number and End IP Number are calculated based on following formula:

IP Number = 16777216*w + 65536*x + 256*y + z     (1)

where
IP Address = w.x.y.z


For example, if IP address is "169.6.7.20", then its IP Number "2835744532" is based on the formula (1).

IP Address = 169.6.7.20

So, w = 169, x = 6, y = 7 and z = 20

IP Number = 16777216*169 + 65536*6 + 256*7 + 20
          = 2835349504 + 393216 + 1792 + 20
          = 2835744532


PHP Function To Convert IP Address to IP Number
-----------------------------------------------
function Dot2LongIP ($IPaddr)
{
if ($IPaddr == "") {
return 0;
} else {
$ips = split ("\.", "$IPaddr");
return ($ips[3] + $ips[2] * 256 + $ips[1] * 65536 + $ips[0]
*16777216); }
}

ASP Function To Convert IP Address to IP Number
-----------------------------------------------
Function Dot2LongIP (ByVal DottedIP)
Dim i, pos
Dim PrevPos, num
If DottedIP = "" Then
Dot2LongIP = 0
Else
For i = 1 To 4
pos = InStr(PrevPos + 1, DottedIP, ".", 1)
If i = 4 Then
pos = Len(DottedIP) + 1
End If
num = Int(Mid(DottedIP, PrevPos + 1, pos - PrevPos - 1))
PrevPos = pos
Dot2LongIP = ((num Mod 256) * (256 ^ (4 - i))) + Dot2LongIP
Next
End If
End Function

Java - Convert IP address to Decimal Number
-----------------------------------------------
public long ipToLong(String ipAddress) {
String[] ipAddressInArray = ipAddress.split("\\.");
long result = 0;
for (int i = 0; i < ipAddressInArray.length; i++) {
int power = 3 - i;
int ip = Integer.parseInt(ipAddressInArray[i]);
result += ip * Math.pow(256, power);
}
return result;
}

C# - Convert an IPv4 address into a integer
-----------------------------------------------
public double IPAddressToNumber(string IPaddress)
{
int i;
string [] arrDec;
double num = 0;
if (IPaddress == "")
{
return 0;
}
else
{
arrDec = IPaddress.Split('.');
for(i = arrDec.Length - 1; i >= 0 ; i = i -1)
{
num += ((int.Parse(arrDec[i])%256) * Math.Pow(256 ,(3 - i )));
}
return num;
}
}

 If you have a question that is not answered here, please e-mail it to techsupport@ip2country.net.

Copyright ©2001-2017 IP2Country.net All rights reserved.