IP地址分段計算 (轉)

amyz發表於2007-11-16
IP地址分段計算 (轉)[@more@]

/* written by Jaron ,2000-01-10 */
/* 原出處:江都資訊網 /"> */
/* 轉載請註明出處和保留此版權資訊 */
/* 歡迎使用SiteManager網站管理  */
/*相關文章:?id=19652*/">*/


在vbs中沒有位操作,這樣在一個頁面中用到了js和vbs,並不好,如果用vbs也可以,不過羅嗦了一些,而且有一點注意,如果在vbs中split("202.102.29.6",","),會得到202,102,29三個數,得不到最後一個6,所以需要將ip換成split("202.102.29.6" & ".",",")
我用vbs做的,由於沒有位操作,所以做得比較麻煩
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="202.102.29.6"
testInt = ip2int(testIP)
response.write testIP & " will be encoded to " & testInt & "
"
response.write testIP & " will be dencoded to " & int2ip(testInt) & "
"
%>


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-982817/,如需轉載,請註明出處,否則將追究法律責任。

相關文章