decToBinValList(decVal)
Last updated November 03, 2005
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;
}
Search CFLib.org
Latest Additions
Raymond Camden added
QueryDeleteRows
November 04, 2017
Leigh added
nullPad
May 11, 2016
Raymond Camden added
stripHTML
May 10, 2016
Kevin Cotton added
date2ExcelDate
May 05, 2016
Raymond Camden added
CapFirst
April 25, 2016