CFLib.org – Common Function Library Project

AsciiToDec(string [, order] [, signed])

Last updated January 2, 2003

Version: 2 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 0 time(s). Average Rating: 0

Description:
Convert ASCII characters (i.e. binary file) into a decimal number. You can specify either Intel or Motorola byte order, and process signed intergers normally or in two's complement notation.

Return Values:
Returns a string.

Example:

view plain print about
<cfset ascii = "!ý?ý?ý?ý?ý?ý?ý?¿">
<cfoutput>
Unsigned Intel: #AsciiToDec(ascii)#<br>
Signed Intel: #AsciiToDec(ascii, "i", true)#<br>
TCN signed Intel: #AsciiToDec(ascii, "i", "tcn")#<br>
Unsigned Motorola: #AsciiToDec(ascii, "m")#
</cfoutput>

Parameters:

Name Description Required
string String to format. Yes
order Byte order (i for Intel or m for Motorola) No
signed Process signed integers normally or in two's complement notation. Values are false (process normally), true (signed), and tcn (2's complement notation) No

Full UDF Source:

view plain print about
<cfscript>
/**
 * Convert ASCII characters into a decimal number.
 * Removed evaluate
 * 
 * @param string      String to format. (Required)
 * @param order      Byte order (i for Intel or m for Motorola) (Optional)
 * @param signed      Process signed integers normally or in two's complement notation. Values are false (process normally), true (signed), and tcn (2's complement notation) (Optional)
 * @return Returns a string. 
 * @author Evan Keller (coldfusion@evankeller.com) 
 * @version 2, January 2, 2003 
 */

function AsciiToDec(string) {
    var order="i";        //Optional arrtibute: Byte Order
                        //"i"= Intel (default)
                        //"m"= Motorola
    var signed=false;    //Optional attribute: Signed
                        //false= unsigned (default)
                        //true= signed
                        //"tcn"= 2's Complement Notation
    var result=0;
    var i=0;
    
    if (ArrayLen(arguments) gt 1) {
        order = arguments[2];
    }
    if (ArrayLen(arguments) gt 2) {
        signed = arguments[3];
    }
    for (i=1; i LTE len(string)+1; i=i+1) {
        if (order is "i"{
            result = result + (asc(mid(string, i, 1)) * 256^(i-1));
        }
        if (order is "m"{
            result = result + (asc(mid(string, i, 1)) * 256^(len(string)-i));
        }
    }
    switch (signed) {
        case true:
            if (len(string) is 0) { //If the string is "0" the length is calculated as zero,
                                    //which throws things off, we set the string to " " so
                                    //it has a length of one.
                string = " ";
            }
            result = result - 256^len(string)/2;
        case "tcn":
            if (result GTE 256^len(string)/2) {
                result = result - 256^len(string);
            }
        default:
            result = result;
    }
    return result;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
4 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

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

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson