Creating ASCII characters in ASL

Overview

 

We're sometimes asked "Is there is an existing function (in ASL) that can convert an ascii number (in decimal) to its corresponding value, e.g. 33 -> !, 49 -> 1??"

 

There is no such function built into ASL, but we can write our own function to accomplish this.

 

The Code

// These two tables are created in the static section
// asciiList will contain the ascii values for 0 - 255

asciiList = list();
hexDigits = list('0','1','2','3',
                 '4','5','6','7',
                 '8','9','A','B',
                 'C','D','E','F');
i = 0;


// This builds up asciiList with the ascii character values 0 - 255
foreach D1 (hexDigits) {
    foreach D2 (hexDigits) {
        digit = D1.D2;
        asciiList+ = hexToString(digit); 
        i = i + 1;
    }
}

START {
    ..eol
} do {
    x = 0;
    while (x < 256) {
        y = digitToAscii(x);
        print("x: ".x.", ascii(x): ".y);
        x = x + 1;
    }
}

// This function takes an integer and returns the ASCII character
// corresponding to the integer.  Note that passing this function a
// value outside of 0 to 255 will generate a null (ASCII 0)

digitToAscii(myDigit) {
    input = myDigit;

    i : integer eol        // Verify that the input is an integer

} do {
    if (i > 255 || i < 0) {
        i = 0;
    }
    return asciiList[myDigit];
}

 

 

About the author

 

David Fiore works in EMC's Resource Management Software Group.