CFLib.org – Common Function Library Project

TwosCompToDec(num [, bits])

Last updated May 31, 2002
Download UDF

author

Stephen Rittler                                   Stephen Rittler

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

 
Rated 2 time(s). Average Rating: 3.5

Description:
This function will convert any 2's complement encoded base 10 value into an unencoded base 10 value, provided you specify the value and the number of bits the decimal value originally represented.

Return Values:
Returns a numeric value.

Example:

<cfset x = -49>
<cfoutput>
Given x=#x#<BR>
The original decimal value is: #TwosCompToDec(x, 8)#
</cfoutput>

Parameters:

Name Description Required
num 2's complement you want to convert back to its original decimal value.. Yes
bits Number of bits in the original decimal value. No

Full UDF Source:

<cfscript>
/**
* Decodes a 2's complement base 10 (decimal) value into an unencoded base 10 value.
*
* @param num      2's complement you want to convert back to its original decimal value.. (Required)
* @param bits      Number of bits in the original decimal value. (Optional)
* @return Returns a numeric value.
* @author Stephen Rittler (scrittler@etechsolutions.com)
* @version 1, May 31, 2002
*/

function TwosCompToDec(num, bits){
    var isNegative = 0;
    var varBase10 = num;
    var varBase2 = 0;
    var varBase2Rev = "";
    var strLen=0;

    // capture whether or not the input value is negative
    if (varBase10 LT 0) {
        isNegative = 1;
        varBase10 = abs(varBase10);
    }
    
    // convert to base 2
    varBase2 = NumberFormat(Right(FormatBaseN(varBase10,2), bits), RepeatString("0", bits));
    
    // switch sign bit to 0
    varBase2 = "0" & Right(varBase2, (bits - 1));

    if(isNegative) {
        // if the number was negative, we have to invert the value and add one
        
        // invert; swap 0 for 1 and 1 for 0
        strLen = len(varBase2);
        for(i=1; i LE bits; i=i+1){
            if(Mid(varBase2, i, 1) eq 0) {
                varBase2Rev = varBase2Rev & 1;
            } else {
                varBase2Rev = varBase2Rev & 0;
            }
        }

        // convert back to base 10
        varBase10 = InputBaseN(varBase2Rev, 2);
        
        //add 1
        return (varBase10 + 1);

    } else {
        // if number was positive, convert straight back to base 10
        return InputBaseN(varBase2, 2);
    }
}
</cfscript>

Search CFLib.org


Latest Additions

Shawn Porter Shawn Porter added
DeMoronize
3 hour(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
1 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
3 day(s) ago

Frank Marion Frank Marion added
sitemapPing
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson