AsciiToDec(string [, order] [, signed])
Last updated January 2, 2003
Version: 2 | Requires: ColdFusion 5 | Library: StrLib
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:
<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
Tayo Akinmade added
arrayTrim
4 day(s) ago
Will Belden added
longTime
9 day(s) ago
James Sleeman added
quickSort
19 day(s) ago
Ben Forta added
GetHostAddress
22 day(s) ago
Top Rated
EksporSQLData
Rated 5.0, 16 time(s)
backupDatabase
Rated 5.0, 13 time(s)
indentXml
Rated 5.0, 10 time(s)
generateSsccAsn
Rated 5.0, 4 time(s)