asp下IP地址分段計算函數

來源:互聯網
上載者:User

IP地址分段計算
<script language="JScript" Runat="Server">
function IPDeCode(EIP){
var Ip1,Ip2,Ip3,Ip4;
Ip1 = moveByteR(EIP & 0xff000000,3);
Ip2 = moveByteR(EIP & 0x00ff0000,2);
Ip3 = moveByteR(EIP & 0x0000ff00,1);
Ip4 = EIP & 0x000000ff;
return Ip1 + "." + Ip2 + "." + Ip3 + "." + Ip4;
}

function moveByteL(num,bytenum){
return num <<= (bytenum*8)
}

function moveByteR(num,bytenum){
return num >>>= (bytenum*8)
}

</script>

在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 <font color=red>" & testInt & "</font><br>"
response.write testIP & " will be dencoded to <font color=red>" & int2ip(testInt) & "</font><br>"
%>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.