JavaScript ip地址转换为二进制

快乐打工仔 分类:实例代码

本章节分享一段代码实例,它实现了将ip地址转换为二进制效果。

代码实例如下:

var praseIpToBinary = function(ipAddress){
  var numArray = ipAddress.split(".");
  if(numArray.length != 4){
    alert("输入的IP有误");
    return;
  }
  var returnIpStr = "";
  for (var i = 0; i < 4; i++) {
    var curr_num = numArray[i];
    var number_Bin = parseInt(curr_num);
    number_Bin = number_Bin.toString(2);
    var iCount = 8-number_Bin.length;
    for (var j = 0; j < iCount; j++) {
      number_Bin = "0"+number_Bin;
    }
    returnIpStr += number_Bin;
  }
  return returnIpStr;
}
var ip="192.168.0.123";
console.log(praseIpToBinary(ip));
网站出售中,有意者加微信:javadudu

回复

我来回复
  • 暂无回复内容