您的位置:首页 > 其它

Ip地址与十进制转化

2011-03-07 18:07 309 查看
<html>

<head>

<title>IP转换</title>

<script>

function ip2long()

{

var ipv4=document.getElementById('aa').value;

var aIPsec=ipv4.split(".");

for(var i=0;i<4;i++)

{

if(parseInt(aIPsec[i])<16)

{

aIPsec[i]="0"+parseInt(aIPsec[i]).toString(16);

}

else

{

aIPsec[i]=parseInt(aIPsec[i]).toString(16);

}

}

var nIPaddr=parseInt("0x"+aIPsec[0]+aIPsec[1]+aIPsec[2]+aIPsec[3]);

document.getElementById('a').innerHTML=nIPaddr;

}

function long2ip ()

{

var proper_address=document.getElementById('bb').value;

var output = false;

if ( !isNaN ( proper_address ))

{

output = ( (proper_address>>24) & 0xff ) + '.' +( (proper_address>>16) & 0xff ) + '.' +( (proper_address>>8) & 0xff ) + '.' +( proper_address & 0xff );

}

document.getElementById('b').innerHTML=output;

}

</script>

</head>

<body>

<input type="text" id="aa" /><input type="button" onclick="ip2long()" value="IP转换为十进制" />

<div id='a'></div>

<input type="text" id="bb" /><input type="button" onclick="long2ip ()" value="十进制转换为IP" />

<div id='b'></div>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: