function ipToNumber(ip) {
var ips = 0, numbers = ip.split(".");
// return parseInt(numbers[0])*256*256*256 +
// parseInt(numbers[1])*256*256 +
// parseInt(numbers[2])*256 +
// parseInt(numbers[3]);
//等价上面
for (var i = 0; i < 4; ++i) {
ips = ips << 8 | parseInt(numbers[i]);
}
return ips;
}
function numberToIp(number) {
// return (Math.floor(number/(256*256*256))) + "." +
// (Math.floor(number%(256*256*256)/(256*256))) + "." +
// (Math.floor(number%(256*256)/256)) + "." +
// (Math.floor(number%256));
//等价上面
var ip = [];
for (var i = 3; i >= 0; i--) {
ip[i] = number & 0xff;
number = number >> 8;
}
return ip.join(".");
}
JavaScript:ip与Integer转换
最新推荐文章于 2022-03-09 16:14:44 发布