知识大全 将IP地址转换为整型数字的PHP方法、Asp方法和MsSQL方法、MySQL方法

Posted 数字

篇首语:时人不识凌云木,直待凌云始道高。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 将IP地址转换为整型数字的PHP方法、Asp方法和MsSQL方法、MySQL方法相关的知识,希望对你有一定的参考价值。

将IP地址转换为整型数字的PHP方法、Asp方法和MsSQL方法、MySQL方法  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  首先我们要先了解一下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 

cha138/Article/program/PHP/201311/21328

相关参考