PHP, Asp, MsSQL, and MySQL methods for converting IP addresses to integer numbers

Source: Internet
Author: User
This article provides a detailed analysis of the PHP, Asp, MsSQL, and MySQL methods for converting IP addresses to integer numbers. For more information, see

This article provides a detailed analysis of the PHP, Asp, MsSQL, and MySQL methods for converting IP addresses to integer numbers. For more information, see

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. for z, server space, website space, and website space, the formula for converting IP addresses to integer numbers is: intIP = 256*256*256 * w + 256*256 * x + 256 * y + z

[PHP conversion]: The PHP conversion method is relatively simple. It has two built-in functions.
Int ip2long (string $ ip_address) and string long2ip (string $ proper_address)
Can be called directly ~

[Asp interchange]: custom functions are as follows,
'.-----------------------------------------------------------.
'| Describtion: converts an IP address to an int number. |
'| Authors: abandonship () |
'~ -----------------------------------------------------------~
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 () |
'~ -----------------------------------------------------------~
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 () |
**************************************** **********************/
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 () |
**************************************** **********************/
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 conversion]: Compared with MsSQL, MySQL conversion is relatively simple. Like PHP, it 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 ~

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.