首页 > 作文

将IP地址转换为整型数字的PHP方法、Asp方法和MsSQL方法、MySQL方法

更新时间:2023-04-06 09:54:01 阅读: 评论:0

首先我们要先了解一下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 = lef电子生日卡片t(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
t @nip = 0
lect
@nip = @nip + left( @strip, charindex(初中生英语怎么说216;.’,@strip+’.’)-1)*id
from(
lect id = cast(1*256*256*256 as bigint)
union all lect 1*256*256
union all lect 1*256
union all lect 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)
t @strip = ”
lect
@strip = @strip +’.’+ cast(@nip/id as varchar), @nip = @nip%id
fr两个月亮om(
lect id = cast(1*256*256*256 as bigint)
union all lect 1*256*256
union all lect 1*256
union all lect 1
) as t
return(st雨巷戴望舒uff(@strip,1,1,”))
end

【mysql的互转】:相对于mssql来说mysql的转换方式比较简单,它和php一样也内置了两个函数
ip转为整型: lect inet_aton (ip地址) 和 整型转为ip: 罪案终结lect inet_ntoa ( ip的整型数值 )
可以直接调用使用~

本文发布于:2023-04-06 09:53:59,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/993e3dd1651e07987dea962a15f032ec.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:将IP地址转换为整型数字的PHP方法、Asp方法和MsSQL方法、MySQL方法.doc

本文 PDF 下载地址:将IP地址转换为整型数字的PHP方法、Asp方法和MsSQL方法、MySQL方法.pdf

标签:转换为   整型   数字   函数
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图