把 ip 转换为整数

原理:先把 192.168.1.13 变成16进制的 c0.a8.01.0d ,再去了“.”后转成10进制的 3232235789 即可。

<%
function ip2int(ipstr)
 dim iptemp,max
 iptemp = split(ipstr&".",".")
 max = ubound(iptemp)
 if max <> 4 then
 exit function
end if

dim a,b,i
a = "&H"
for i = 0 to 3
 b = Hex(iptemp(i))
 if len(b) = 1 then
  b = "0"&b
 end if
 a = a&b
next
ip2int = CLng(a)
end function

function int2ip(ip)
 dim iptemp,a,ipstr,i,length
 iptemp = Hex(ip)
 length = 8 - len(iptemp)
 for i = 1 to length
  iptemp = "0" & iptemp
 next
 a = left(iptemp,2)
 a = "&H" & a
 i = CInt(a)
 a = CStr(i)
 ipstr = a & "."
 a = mid(iptemp,3,2)
 a = "&H" & a
 i = CInt(a)
 a = CStr(i)
 ipstr = ipstr & a & "."
 a = mid(iptemp,5,2)
 a = "&H" & a
 i = CInt(a)
 a = CStr(i)
 ipstr = ipstr & a & "."
 a = right(iptemp,2)
 a = "&H" & a
 i = CInt(a)
 a = CStr(i)
 ipstr = ipstr & a
 int2ip = ipstr
end function

dim testIP,testInt
testIP="0.0.1.0"
testInt = ip2int(testIP)
response.write testIP & " will be encoded to <font color=red>" & testInt & "</font><br>"
response.write testIP & " will be decoded to <font color=red>" & int2ip(testInt) & "</font><br>"
%>

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据