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 |