decToBinValList(decVal)
Last updated November 3, 2005
Version: 1 | Requires: ColdFusion 5 | 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:
<cfoutput>#decToBinValList(x)#</cfoutput>
Parameters:
| Name | Description | Required |
|---|---|---|
| decVal | Decimal value. | Yes |
Full UDF Source:
<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>
Search CFLib.org
Latest Additions
Dave Anderson added
iniToStruct
20 day(s) ago
Dave Anderson added
deDupeArray
20 day(s) ago
Richard added
dice
22 day(s) ago
Isaac Dealey added
getRelative
a while ago
Top Rated
backupDatabase
Rated 5.0, 22 time(s)
indentXml
Rated 5.0, 10 time(s)
generateSsccAsn
Rated 5.0, 4 time(s)
highlightAndCrop
Rated 5.0, 4 time(s)