Function for subnet mask
I have a subnet mask as a value in an object. It comes in a long form, i.e. 255.255.255.0 (for / 24).
I haven't come across any JavaScript function to calculate this. So before writing a lot of if
and statements else if
, I want to quickly double check to make sure I haven't missed any JavaScript helper that does this already.
Thank!
Edit: Clarification
I was wondering if there is a JavaScript function I don't know about that will translate long form and return short form, slash notation. For example:
If passed var obj_mask = "255.255.255.0";
to existing JavaScript (API?) It will return a value /24
.
If such a function doesn't exist in JavaScript, that's good, I've already written half of the operators if
, and I'll be happy to share it after no one else has to write it. But seeing that I'm new to JS, I wanted to know if such a function / API exists natively for the language.
source to share
Just ended up writing and then realized it would look cleaner with switch case
:
For any link. If there is actually some JS or API function that would make this much cleaner and easier, please reply and I'll tag it.
switch(ret_mask = path.mask) {
case "0.0.0.0":
return ret_mask = "/0";
case "128.0.0.0":
return ret_mask = "/1";
case "192.0.0.0":
return ret_mask = "/2";
case "224.0.0.0":
return ret_mask = "/3";
case "240.0.0.0":
return ret_mask = "/4";
case "248.0.0.0":
return ret_mask = "/5";
case "252.0.0.0":
return ret_mask = "/6";
case "254.0.0.0":
return ret_mask = "/7";
case "255.0.0.0":
return ret_mask = "/8";
case "255.128.0.0":
return ret_mask = "/9";
case "255.192.0.0":
return ret_mask = "/10";
case "255.224.0.0":
return ret_mask = "/11";
case "255.240.0.0":
return ret_mask = "/12";
case "255.248.0.0":
return ret_mask = "/13";
case "255.252.0.0":
return ret_mask = "/14";
case "255.254.0.0":
return ret_mask = "/15";
case "255.255.0.0":
return ret_mask = "/16";
case "255.255.128.0":
return ret_mask = "/17";
case "255.255.192.0":
return ret_mask = "/18";
case "255.255.224.0":
return ret_mask = "/19";
case "255.255.240.0":
return ret_mask = "/20";
case "255.255.248.0":
return ret_mask = "/21";
case "255.255.252.0":
return ret_mask = "/22";
case "255.255.254.0":
return ret_mask = "/23";
case "255.255.255.0":
return ret_mask = "/24";
case "255.255.255.128":
return ret_mask = "/25";
case "255.255.255.192":
return ret_mask = "/26";
case "255.255.255.224":
return ret_mask = "/27";
case "255.255.255.240":
return ret_mask = "/28";
case "255.255.255.248":
return ret_mask = "/29";
case "255.255.255.252":
return ret_mask = "/30";
case "255.255.255.254":
return ret_mask = "/31";
case "255.255.255.255":
return ret_mask = ""
}
source to share
I use the following functions to convert netmask to CIDR and CIDR to netmask:
var netmask2CIDR = (netmask) => (netmask.split('.').map(Number)
.map(part => (part >>> 0).toString(2))
.join('')).split('1').length -1;
var CIDR2netmask = (bitCount) => {
var mask=[];
for(var i=0;i<4;i++) {
var n = Math.min(bitCount, 8);
mask.push(256 - Math.pow(2, 8-n));
bitCount -= n;
}
return mask.join('.');
}
Hope this helps!
source to share
Try ...
/* dotted-quad IP to integer */
function IPv4_dotquadA_to_intA( strbits ) {
var split = strbits.split( '.', 4 );
var myInt = (
parseFloat( split[0] * 16777216 ) /* 2^24 */
+ parseFloat( split[1] * 65536 ) /* 2^16 */
+ parseFloat( split[2] * 256 ) /* 2^8 */
+ parseFloat( split[3] )
);
return myInt;
}
/* integer IP to dotted-quad */
function IPv4_intA_to_dotquadA( strnum ) {
var byte1 = ( strnum >>> 24 );
var byte2 = ( strnum >>> 16 ) & 255;
var byte3 = ( strnum >>> 8 ) & 255;
var byte4 = strnum & 255;
return ( byte1 + '.' + byte2 + '.' + byte3 + '.' + byte4 );
}
source to share