php中如何把一个ip地址转换为长整型,将IP地址转换为整型数字的PHP方法、Asp方法和MsSQL方法、MySQL方法...

本文介绍了如何在PHP、ASP、MSSQL和MySQL中将IP地址转换为整型数字以及反之。PHP提供了iplong和longip函数,ASP中通过自定义函数IPNum和NumIP实现转换,而在MSSQL中,创建了两个自定义函数ipToInt和intToIP来完成这一操作。这些方法遵循将IP地址拆分为四部分并进行乘幂运算的转换原理。
摘要由CSDN通过智能技术生成

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

97a735e96039dd35b74c5a07e10c45f5.png

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

分页:123

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值