最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

首先我們要先了解一下IP地址轉(zhuǎn)換為整型(嚴格來說應該說是長整型)的原理~

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

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

相關文章

最新評論

丹江口市| 乃东县| 宁安市| 涿州市| 越西县| 海兴县| 中山市| 广南县| 宾阳县| 安徽省| 高碑店市| 鹤岗市| 开化县| 马公市| 年辖:市辖区| 教育| 合山市| 凌源市| 渝中区| 保康县| 晴隆县| 张家港市| 东港市| 晋宁县| 开化县| 筠连县| 商都县| 金乡县| 喜德县| 西盟| 虎林市| 永丰县| 平舆县| 武隆县| 曲靖市| 墨竹工卡县| 开化县| 东至县| 巢湖市| 玉环县| 桐城市|