CFLib.org – Common Function Library Project

cidrToNetMask(cidr)

Last updated July 19, 2005

Version: 1 | Requires: ColdFusion 5 | Library: NetLib

 
Rated 0 time(s). Average Rating: 0

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

Return Values:
Returns a string.

Example:

view plain print about
#cidrToNetMask(24)#<br>
#cidrToNetMask(12)#<br>

Parameters:

Name Description Required
cidr CIDR number. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * 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;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Adam Cameron Adam Cameron added
composeDateTime
18 day(s) ago

Chris Weller Chris Weller added
convertQueryStri...
a while ago

Greg Nettles Greg Nettles added
arrayDiff
a while ago

Nathan Dintenfass Nathan Dintenfass added
ArrayOfStructsSo...
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 36 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Rachel Lehman deAccent
Rated 5.0, 6 time(s)

Isaac Dealey                                      countArbitraryDa...
Rated 5.0, 5 time(s)

Created by Raymond Camden / Design by Justin Johnson