Js IP转数字

<script type="text/javascript">
function d2h(d) {
return d.toString(16)
}
function h2d(d) {
return parseInt(d, 16)
}
function dot2num(d) {
var n = d.split(".");
return 256 * (256 * (256 * +n[0] + +n[1]) + +n[2]) + +n[3]
}
function num2dot(d) {
for (var n = d % 256, t = 3; t > 0; t--)d = Math.floor(d / 256), n = d % 256 + "." + n;
return n
}
function convert() {
var d = document.getElementById("ipnum").value, n = /^(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])$/, t = /^\d+$/;
d.match(n) ? d = dot2num(d) : d.match(t) && 4294967296 > +d && (d = num2dot(d)), document.getElementById("ipnum").value = d
}
</script>

 

posted @ 2016-12-20 14:33  小小白鼠  阅读(1653)  评论(0编辑  收藏  举报