PHP method, Asp method, MsSQL method, MySQL method to convert IP address to integer number_PHP tutorial

WBOY
Release: 2016-07-21 15:01:12
Original
801 people have browsed it

首先我们要先了解一下IP地址转换为整型(严格来说应该说是长整型)的原理~

【转换原理】:假设IP为:w.x.y.z,则IP地址转为整型数字的计算公式为:intIP = 256*256*256*w + 256*256*x + 256*y + z

【PHP的互转】:PHP的转换方式比较简单,它内置了两个函数
int ip2long ( string $ip_address )和 string long2ip ( string $proper_address )
可以直接调用使用~

【Asp的互转】:自定义函数如下,
'.-----------------------------------------------------------.
'|  describtion: 将IP转换为int型数字                           |
'|      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型数字转换为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的互转】:自定义函数如下,
/***************************************************************
 * 将IP转换为int型数字                         |
 * 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型数字转换为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 conversion]: Compared to MsSQL, MySQL conversion method is relatively simple. Like PHP, it also has two built-in functions
IP to integer: select INET_ATON ( IP address) and convert integer to IP: select INET_NTOA (integer value of IP)
can be called and used directly~

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328016.htmlTechArticleFirst we need to understand how the IP address is converted into an integer (strictly speaking, it should be a long integer) Principle~ [Conversion Principle]: Suppose the IP is: w.x.y.z, then the calculation of converting the IP address into an integer number...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!