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

Source: Internet
Author: User
Tags mssql

First of all, we need to understand the IP address conversion to the integral type (strictly speaking is a long integer) the principle of ~

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

"PHP to go with each other": PHP is a simpler way to convert, with two functions built into it
int Ip2long (string $ip _address) and string Long2ip (String $proper _address)
Can be directly invoked using ~

"ASP's Mutual transfer":The custom functions are as follows,
'.-----------------------------------------------------------.
'| Describtion: CONVERT IP to int-type numbers |
'| 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: Convert int numbers 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/(256 ^ nindex))
Strip = strip & Ntemp & "."
NIP = NIP-(ntemp * (256 ^ nindex))
Next
Strip = Left (strip, Len (strip)-1)
Num2ip = StrIP
End Function

"MSSQL": 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": compared to the MSSQL MySQL conversion mode is relatively simple, it and PHP as well as built-in two functions
IP to integral type: Select Inet_aton (IP address) and integer to Ip:select inet_ntoa (IP integer value)
Can be directly invoked using ~

Related Article

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.