I found some code in PHP to convert CIDR to netmask.
function createNetmaskAddr($bitcount) {
$netmask = str_split(str_pad(str_pad('', $bitcount, '1'), 32, '0'), 8);
foreach ($netmask as &$element) $element = bindec($element);
return join('.', $netmask);
}
Is it possible to do it in javascript?
Unfortunately I have no experience in javascript.
Try this:
function createNetmaskAddr(bitCount) {
var mask = [], i, n;
for(i=0; i<4; i++) {
n = Math.min(bitCount, 8);
mask.push(256 - Math.pow(2, 8-n));
bitCount -= n;
}
return mask.join('.');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With