First of all, we need to understand the IP address conversion to the integral type (strictly speaking is a long integer) the principle of ~
"Principle of conversion": assuming the IP is: w.x.y.z, the IP address to the integer number of the calculation formula: Intip = 256*256*256*w + 256*256*x + 256*y + Z
"PHP to go with each other": PHP is a simpler way to convert, with two functions built into it
int Ip2long (string $ip _address) and string Long2ip (String $proper _address)
Can be directly invoked using ~
"ASP's Mutual transfer":The custom functions are as follows,
'.-----------------------------------------------------------.
'| Describtion: CONVERT IP to int-type numbers |
'| Authors:abandonship (http://jb51.net) |
'~-----------------------------------------------------------~
Function ip2num (ByVal StrIP)
Dim NIP
Dim nindex
Dim Arrip
Arrip = Split (StrIP, ".", 4)
For nindex = 0 to 3
If not nindex = 3 Then
Arrip (nindex) = Arrip (nindex) * (256 ^ (3-nindex))
End If
NIP = NIP + Arrip (nindex)
Next
Ip2num = NIP
End Function
'.-----------------------------------------------------------.
'| Describtion: Convert int numbers to IP |
'| Authors:abandonship (http://jb51.net) |
'~-----------------------------------------------------------~
Function Num2ip (ByVal NIP)
Dim StrIP
Dim ntemp
Dim nindex
For nindex = 3 to 0 Step-1
Ntemp = Int (NIP/(256 ^ nindex))
Strip = strip & Ntemp & "."
NIP = NIP-(ntemp * (256 ^ nindex))
Next
Strip = Left (strip, Len (strip)-1)
Num2ip = StrIP
End Function
"MSSQL": the Custom function is as follows,
/***************************************************************
* Convert IP to INT-type number |
* Code CreateBy abandonship (http://jb51.net) |
**************************************************************/
CREATE FUNCTION [dbo]. [Iptoint] (
@strIp varchar (15)
) RETURNS bigint
As
BEGIN
DECLARE @nIp bigint
Set @nIp = 0
Select
@nIp = @nIp + Left (@strIp, charindex ('. ', @strIp + '. ') -1) *id
From
Select Id = Cast (1*256*256*256 as bigint)
UNION ALL Select 1*256*256
UNION ALL Select 1*256
UNION ALL Select 1
) as T
Return (@nIp)
End
/***************************************************************
* Convert int numbers to IP |
* Code CreateBy abandonship (http://jb51.net) |
**************************************************************/
CREATE FUNCTION [dbo]. [Inttoip] (
@nIp bigint
) RETURNS varchar (15)
As
BEGIN
DECLARE @strIp varchar (15)
Set @strIp = '
Select
@strIp = @strIp + '. ' + CAST (@nIp/id as varchar), @nIp = @nIp%id
From
Select ID = Cast (1*256*256*256 as bigint)
UNION ALL Select 1*256*256
UNION ALL Select 1*256
UNION ALL Select 1
) as T
Return (Stuff (@strIp, 1, 1, '))
End
"MySQL": compared to the MSSQL MySQL conversion mode is relatively simple, it and PHP as well as built-in two functions
IP to integral type: Select Inet_aton (IP address) and integer to Ip:select inet_ntoa (IP integer value)
Can be directly invoked using ~