Extreme Thinking
十六进制转换为IP地址

2019-01-19


<!DOCTYPE html>
<head>
<meta charset="gb2312">
<title>十六进制 IP地址转换</title>

</head>
<body>
      <SCRIPT type=text/javascript>
function hex(){
        var ipv = document.getElementById("inp2").value;
        ipv = ipv.toUpperCase();
        if(ishex(ipv)){
                ipv = parseInt(ipv,16)
                var b = new Array();
                b[0] =0;
                b[1] =0;
                b[2] =0;
                b[3] =0;
                var c = 16777216.0;
            for (var i = 0; i < 4; i++) {
           var k = parseInt(ipv / c);
               ipv -= c * k;
           b[i]= k;
           c = c/256.0;
            }
        d=b[0]+'.'+b[1]+'.'+b[2]+'.'+b[3];
                document.getElementById("res2").innerHTML = "IP = "+d;
        }else{
                document.getElementById("error2").innerHTML = '无效的十六进制值';
                return false;
        }
}
function ishex(num){
        var validChar='0123456789ABCDEF';
        var flag=true;
        var x=num.toUpperCase();
        for(idx=0;idx<x.length;idx++){
                if(validChar.indexOf(x.charAt(idx))<0){
                        return false;
                }
        }
        return true;
}
</SCRIPT>
            <TABLE style="MARGIN: 0px; WIDTH: 396px">
              <TR>
                <TD id=title vAlign=top colSpan=2 align=middle bgcolor="#E6F2FF">
                  <H3>十六进制转换为IP地址</H3></TD></TR>
              <TR>
                <TD vAlign=top width=70 bgcolor="#E6F2FF">16进制</TD>
                <TD bgcolor="#E6F2FF"><INPUT id=inp2 type=text> </TD></TR>
              <TR>
                <TD align=middle bgcolor="#E6F2FF">
                <TD bgcolor="#E6F2FF"><BUTTON onclick=hex() type=button>转换</BUTTON></TD></TR>
              <TR>
                <TD></TD>
                <TD id=res2></TD></TR></TBODY></TABLE><BR>

</body>
</html>