當前位置:編程學習大全網 - 源碼下載 - 將IP地址轉換為整型數字的PHP方法、Asp方法和MsSQL方法、MySQL方法

將IP地址轉換為整型數字的PHP方法、Asp方法和MsSQL方法、MySQL方法

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

  轉換原理 假設IP為 w x y z 則IP地址轉為整型數字的計算公式為 intIP = * * *w + * *x + *y + z

  PHP的互轉 PHP的轉換方式比較簡單 它內置了兩個函數 int ip long ( string $ip_address )和 string long ip ( string $proper_address ) 可以直接調用使用~

  Asp的互轉 自定義函數如下 |? describtion: 將IP轉換為int型數字? | |? Authors: abandonship| ~ ~ Function IP Num(ByVal strIP) Dim nIP Dim nIndex Dim arrIP arrIP = Split(strIP " " ) For nIndex = To If Not nIndex = Then arrIP(nIndex) = arrIP(nIndex) * ( ^ ( nIndex)) End If nIP = nIP + arrIP(nIndex) Next IP Num = nIP End Function |? describtion: 將int型數字轉換為IP? | |? Authors: abandonship| ~ ~ Function Num IP(ByVal nIP) Dim strIP Dim nTemp Dim nIndex For nIndex = To Step ?nTemp = Int(nIP / ( ^ nIndex)) ?strIP = strIP & nTemp & " " ?nIP = nIP (nTemp * ( ^ nIndex)) Next strIP = Left(strIP Len(strIP) ) Num IP = strIP End Function

  MsSQL的互轉 自定義函數如下 /*************************************************************** ?*?將IP轉換為int型數字 | ?*?Code CreateBy abandonship| ?**************************************************************/ CREATE FUNCTION [dbo] [ipToInt]( @strIp varchar( )? )RETURNS bigint? AS? BEGIN declare @nIp bigint set @nIp = ?select ?@nIp = @nIp + LEFT( @strIp charindex( @strIp+ ) )*Id from( select Id = cast( * * * as bigint) union all select * * union all select * union all select ?) as T ?return (@nIp) END? /*************************************************************** ?*?將int型數字轉換為IP | ?*?Code CreateBy abandonship| ?**************************************************************/ CREATE FUNCTION [dbo] [intToIP]( ?@nIp bigint? )RETURNS varchar( )? As? BEGIN declare @strIp varchar( ) set @strIp = select ?@strIp = @strIp + + cast(@nIp/ID as varchar) @nIp = @nIp%ID ?from( select ID = cast( * * * as bigint) union all select * * union all select * union all select ?) as T return(stuff(@strIp ))? END?

lishixinzhi/Article/program/PHP/201311/21328

  • 上一篇:0成本引流|教妳玩轉FacebookGroup社群運營
  • 下一篇:當年手機界比較牛X的系統有那些,都有何獨到之處?
  • copyright 2024編程學習大全網