CFLib.org – Common Function Library Project

decToBinValList(decVal)

Last updated November 3, 2005

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

 
Rated 0 time(s). Average Rating: 0

Description:
You give me a decimal number, and I return a list of numbers resulting from converting your original value to binary, and returning the non zero results. Example, you give me "13" and I return "1,4,8".

Return Values:
Returns a string.

Example:

view plain print about
<cfset x = 13>
<cfoutput>#decToBinValList(x)#</cfoutput>

Parameters:

Name Description Required
decVal Decimal value. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Converts decimal number to list of binary place values.
 * 
 * @param decVal      Decimal value. (Required)
 * @return Returns a string. 
 * @author Alan McCollough (amccollough@anmc.org) 
 * @version 1, November 3, 2005 
 */

function decToBinValList(decVal) {
    // create an empty counter
    var i = "";        
    // create an empty 'current value'
    var cur = "";
    // convert decimal val to binary
    var bVal = FormatBaseN(val(decVal), 2);
    // set our binary seed to 1, the first place in the binary system
    var b = 1;
    // create an empty list to hold the results
    var resultList = "";
    
    // loop through the places in the binary number, going from right to left.
    for(i = len(bVal); ; i = i - 1) {            
            cur = val(b * mid( bVal , i , 1 ));
            if (cur gt 0) resultList = listAppend(resultList,cur);

            // double the value of our binary seed
            b = 2 * b;
            //exit loop when the last bit is processed    
            if (i eq 1) break;
        }
    
    // return the list    
    return resultList;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

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

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson