CFLib.org – Common Function Library Project

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

Last updated January 2, 2003
Download UDF

author

Evan Keller                                       Evan Keller

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:

<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:

<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>

Search CFLib.org


Latest Additions

Ryan Thompson-Jewell Ryan Thompson-Jewell added
ListSplit
1 day(s) ago

Nathan Dintenfass Nathan Dintenfass added
RowsToColumns
1 day(s) ago

Barney Boisvert Barney Boisvert added
indentXml
1 day(s) ago

Barney Boisvert Barney Boisvert added
REReplaceCallbac...
1 day(s) ago

Top Rated

Rob Brooks-Bilson                                 FolderSize
Rated 5.0, 7 time(s)

Nick Giovanni                                     UniqueValueList
Rated 5.0, 5 time(s)

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Jeff Howden ListDeleteDuplic...
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson