Php method to convert IP address to integer number, ASP method and Mssql method, MySQL method _php tutorial

Source: Internet
Author: User
First of all, we need to understand the principle of IP address conversion to integer (strictly speaking, long integer type) ~

"Principle of conversion": assuming that IP is: w.x.y.z, the IP address to integer number is calculated as: Intip = 256*256*256*w + 256*256*x + 256*y + Z

"PHP's Mutual transfer": PHP is a simple way to convert, with two functions built into it
int Ip2long (string $ip _address) and string Long2ip (String $proper _address)
Can be called directly using the ~

"Inter-Turn of ASP":The custom functions are as follows,
'.-----------------------------------------------------------.
'| Describtion: CONVERT IP to int type number |
'| 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) * (^ (3-nindex))
End If
NIP = NIP + Arrip (nIndex)
Next
Ip2num = NIP
End Function
'.-----------------------------------------------------------.
'| Describtion: Convert int to 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/(NIndex))
StrIP = StrIP & Ntemp & "."
NIP = NIP-(ntemp * (NIndex))
Next
StrIP = Left (StrIP, Len (StrIP)-1)
Num2ip = StrIP
End Function

"MSSQL's mutual transfer": The Custom function is as follows,
/***************************************************************
* Convert IP to int type number |
* 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

/***************************************************************
* Convert int numbers to 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 Mutual turn": compared to MSSQL, MySQL is a simple way to convert, and it has built up two functions as well as PHP.
IP-to-integer: Select Inet_aton (IP address) and integer to Ip:select inet_ntoa (integer value of IP)
Can be called directly using the ~

http://www.bkjia.com/PHPjc/328016.html www.bkjia.com true http://www.bkjia.com/PHPjc/328016.html techarticle first of all we need to understand the IP address conversion to integer (strictly speaking, long integer) principle ~ "Principle of conversion": Assuming that IP is: w.x.y.z, then the IP address to integer number calculation ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.