国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 數(shù)據(jù)庫(kù) > MySQL > 正文

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

2024-07-24 12:43:14
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

首先我們要先了解一下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)用使用~

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 普兰店市| 诸城市| 嘉义县| 望江县| 正宁县| 黔江区| 武威市| 邮箱| 青海省| 靖远县| 彭泽县| 泾阳县| 杨浦区| 十堰市| 广德县| 嘉禾县| 晋中市| 乐至县| 凤翔县| 罗江县| 阳西县| 西乌珠穆沁旗| 龙门县| 通山县| 巴彦县| 五大连池市| 武义县| 阿拉尔市| 文昌市| 旬阳县| 缙云县| 隆回县| 沙雅县| 鹤岗市| 芜湖市| 稷山县| 明星| 姚安县| 炎陵县| 洛宁县| 开化县|