CFLib.org – Common Function Library Project

decToBinValList(decVal)

Last updated November 3, 2005

author

Alan McCollough

Version: 1 | Requires: CF5 | Library: UtilityLib

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:

<cfset x = 13>
<cfoutput>#decToBinValList(x)#</cfoutput>

Parameters:

Name Description Required
decVal Decimal value. Yes

Full UDF Source:

/**
 * 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;
}
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