Ip地址与十进制转化

<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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值