欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

將IP地址轉(zhuǎn)換為整型數(shù)字的PHP方法、Asp方法和MsSQL方法、MySQL方法

 更新時(shí)間:2013年07月03日 10:52:28   作者:  
本篇文章是對(duì)IP地址轉(zhuǎn)換為整型數(shù)字的PHP方法、Asp方法和MsSQL方法、MySQL方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下

首先我們要先了解一下IP地址轉(zhuǎn)換為整型(嚴(yán)格來(lái)說(shuō)應(yīng)該說(shuō)是長(zhǎng)整型)的原理~

【轉(zhuǎn)換原理】:假設(shè)IP為:w.x.y.z,則IP地址轉(zhuǎn)為整型數(shù)字的計(jì)算公式為:intIP = 256*256*256*w + 256*256*x + 256*y + z

【PHP的互轉(zhuǎn)】:PHP的轉(zhuǎn)換方式比較簡(jiǎn)單,它內(nèi)置了兩個(gè)函數(shù)
int ip2long ( string $ip_address )和 string long2ip ( string $proper_address )
可以直接調(diào)用使用~

【Asp的互轉(zhuǎn)】:自定義函數(shù)如下,
'.-----------------------------------------------------------.
'|  describtion: 將IP轉(zhuǎn)換為int型數(shù)字                           |
'|      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: 將int型數(shù)字轉(zhuǎn)換為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的互轉(zhuǎn)】:自定義函數(shù)如下,
/***************************************************************
 * 將IP轉(zhuǎn)換為int型數(shù)字                         |
 * 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 

/***************************************************************
 * 將int型數(shù)字轉(zhuǎn)換為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的互轉(zhuǎn)】:相對(duì)于MsSQL來(lái)說(shuō)MySQL的轉(zhuǎn)換方式比較簡(jiǎn)單,它和PHP一樣也內(nèi)置了兩個(gè)函數(shù)
IP轉(zhuǎn)為整型: select INET_ATON (IP地址) 和 整型轉(zhuǎn)為IP: select INET_NTOA ( IP的整型數(shù)值 )
可以直接調(diào)用使用~

相關(guān)文章

最新評(píng)論