PHP method, Asp method, MsSQL method, and MySQL method _ PHP Tutorial for converting IP addresses to integer numbers

Source: Internet
Author: User
Tags integer numbers
The PHP method, Asp method, MsSQL method, and MySQL method for converting an IP address to an integer. First, we need to first understand the principle of converting an IP address to an integer (in strict terms, it should be a long integer ~ [Conversion principle]: assume that the IP address is w. x. y. z, then the IP address is converted to an integer. First, we need to understand the principle of converting an IP address to an integer (in strict terms, it should be a long integer ~

[Conversion principle ]:Assume that the IP address is w. x. y. z, the formula for converting the IP address to an integer is: intIP = 256*256*256 * w + 256*256 * x + 256 * y + z

[PHP interaction ]:PHP is easy to convert. It has two built-in functions.
Int ip2long (string $ ip_address) and string long2ip (string $ proper_address)
Can be called directly ~

[Asp interchange ]:The custom functions are as follows,
'.-----------------------------------------------------------.
'| Describtion: converts an IP address to an int 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) * (256 ^ (3-nIndex ))
End If
NIP = nIP + arrIP (nIndex)
Next
IP2Num = nIP
End Function
'.-----------------------------------------------------------.
'| Describtion: converts int-type numbers to IP addresses. |
'| 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 interchange]: custom functions are as follows,
/*************************************** ************************
* Convert an IP address to an int 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 (1x256*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-type numbers to IP addresses |
* 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 (1x256*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 interchange ]:Compared with MsSQL, MySQL converts data in a simple way. like PHP, MySQL also has two built-in functions.
Convert IP to integer: select INET_ATON (IP address) and convert integer to IP: select INET_NTOA (integer value of IP address)
Can be called directly ~

The principle of round (it should be a long integer ~ [Conversion principle]: if the IP address is w. x. y. z, the IP address is converted to an integer...

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.