CFLib.org – Common Function Library Project

cidrToNetMask(cidr)

Last updated July 19, 2005

author

Sufiyan bin Yasa

Version: 1 | Requires: CF5 | Library: NetLib

Description:
CIDR numbers such as /24,/32,/12 are converted to the appropiate netmask address form.

Return Values:
Returns a string.

Example:

#cidrToNetMask(24)#<br>
#cidrToNetMask(12)#<br>

Parameters:

Name Description Required
cidr CIDR number. Yes

Full UDF Source:

/**
 * Converts CIDR numbers to valid network mask numbers.
 * 
 * @param cidr 	 CIDR number. (Required)
 * @return Returns a string. 
 * @author Sufiyan bin Yasa (cinod_79@yahoo.com) 
 * @version 1, July 19, 2005 
 */
function cidrToNetMask (cidr) {
	var netMask = "";	

	var post = 0;
	var remainder = cidr MOD 8;
	var divide = cidr \ 8;

	while(divide gt 0) {
		netMask = listAppend(netMask, 255,'.'); 
		divide = divide - 1;
		post = post + 1;		
	}

	if(remainder gt 0) {			
		netMask = listAppend(NetMask,
				  bitSHLN(BitOr(0,2^remainder-1), 8-remainder),
				  '.'); 		
		post = post +1;			
	}

	while(post lt 4) {
		netMask = listAppend(netMask, "0",'.'); 			
		post = post + 1;
	}
	
	if(right(netMask, 1) eq "."){		
		netMask = left(netMask,len(netMask));
	}
	return netMask;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson