Sie sind auf Seite 1von 3

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 that is a power of 256. IP number is being used in the database because it is more
efficient to search between a range of numbers in a database.

The Beginning IP number and Ending IP Number are calculated based on the following
formula:

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

where

IP Address = w.x.y.z

For example, if the IP address is "202.186.13.4", then its IP Number will be


"3401190660", based on the formula (1).
IP Address = 202.186.13.4

So, w = 202, x = 186, y = 13 and z = 4

IP Number = 16777216*202 + 65536*186 + 256*13 + 4


= 3388997632 + 12189696 + 3328 + 4
= 3401190660

To reverse IP number to IP address,


w = int ( IP Number / 16777216 ) % 256
x = int ( IP Number / 65536 ) % 256
y = int ( IP Number / 256 ) % 256
z = int ( IP Number ) % 256

where % is the modulus operator and int returns the integer part of the division.

Example 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
Example 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] * 256 * 256 + $ips[0] * 256 * 256
* 256);
}
}

Example ColdFusion Function To Convert IP Address to IP Number


<cfscript>
function Dot2LongIP(ipAddress)
{
if(arguments.ipAddress EQ "")
{
return 0;
}
else
{
ips = ListToArray( arguments.ipAddress, "." );
return( ( 16777216 * ips[1] ) + ( 65536 * ips[2] ) + ( 256 * ips[3] ) +
ips[4] );
}
}
</cfscript>

Example C# Function To Convert IP Address to IP Number


public double Dot2LongIP(string DottedIP)
{
int i;
string [] arrDec;
double num = 0;
if (DottedIP == "")
{
return 0;
}
else
{
arrDec = DottedIP.Split('.');
for(i = arrDec.Length - 1; i >= 0 ; i --)
{
num += ((int.Parse(arrDec[i])%256) * Math.Pow(256 ,(3 - i )));
}
return num;
}
}
Example VB.NET Function To Convert IP Address to IP Number
Public Function Dot2LongIP(ByVal DottedIP As String) As Double
Dim arrDec() As String
Dim i As Integer
Dim intResult As Long
If DottedIP = "" then
Dot2LongIP = 0
Else
arrDec = DottedIP.Split(".")
For i = arrDec.Length - 1 To 0 Step -1
intResult = intResult + ((Int(arrDec(i)) Mod 256) * Math.Pow(256, 3 -i))
Next
Dot2LongIP = intResult
End If
End function

Example MS SQL Function To Convert IP Address to IP Number


Create FUNCTION [dbo].[Dot2LongIP]( @IP VarChar(15) )
RETURNS BigInt
AS
BEGIN
DECLARE @ipA BigInt,
@ipB Int,
@ipC Int,
@ipD Int,
@ipI BigInt
SELECT @ipA = LEFT(@ip, PATINDEX('%.%', @ip) - 1 )
SELECT @ip = RIGHT(@ip, LEN(@ip) - LEN(@ipA) - 1 )

SELECT @ipB = LEFT(@ip, PATINDEX('%.%', @ip) - 1 )


SELECT @ip = RIGHT(@ip, LEN(@ip) - LEN(@ipB) - 1 )

SELECT @ipC = LEFT(@ip, PATINDEX('%.%', @ip) - 1 )


SELECT @ip = RIGHT(@ip, LEN(@ip) - LEN(@ipC) - 1 )

SELECT @ipD = @ip


RETURN ( @ipA * 256*256*256 ) + ( @ipB * 256*256 ) + ( @ipC * 256 ) + @ipD
END
RETURN @ipI
END

Please visit http://www.ip2location.com for more information about IP address


geolocation products and services.

About IP2Location.com

Founded in 2001, IP2Location.com provides Internet infrastructure intelligence services to online


businesses. IP2Location.com's products provide the geographic location of Web site visitors in
real-time, enabling businesses to display localized content, bandwidth balancing, improve click-
through and sales, prevent fraud, conduct site analysis and foster regulatory compliance. With
over 4000 industry leading customers, IP2Location.com is one of the market leaders in the geo-
location industry. IP2Location.com's main headquarters is located in Penang, Malaysia, with
regional sales office located in Florida, United States.

Das könnte Ihnen auch gefallen