LiveCode LessonsHow To - LiveCode Sample Scripts MiscellaneousConverting between characters and numerical (ASCII) values

Converting between characters and numerical (ASCII) values

This lesson demonstrates how to convert a character or ASCII value (a number between 0 and 255) to its ASCII value or character equivalent.

The covertChar custom command

The custom command convertChar takes a single, optional parameter pConversionType specifying what is being converted (either "character" or "ASCII"). The handler assumes there are 2 fields, one called "character" and one called "ASCII" which hold the character and  its ASCII equivalent.

The convertChar command is used with a statement like the following:

convertChar "ASCII"

convertChar

convertChar "character"

Converting between characters and ASCII

Unlike some languages (such as C) and like others (such as Perl), LiveCode treats individual characters as strings, not as numbers. This means that you can't simply add a number to a character to change its ASCII value. You must first convert the character to its ASCII value. In LiveCode, this is done with the charToNum function. Converting a number to the corresponding character is done with the numToChar function.

The core of the handler consists of these two statements:

put numToChar(it) into field "Character"

put charToNum(field "Character") into field "ASCII"

The first of these statements converts a number to a character; the second converts a character to its ASCII value.

The pConversionType parameter

The handler takes one parameter, which is what's being converted (either "character" or "ASCII"). However, you can call the handler without this parameter, and the handler itself will determine which direction to convert it. It's possible to do this because the first part of the handler checks to see whether the parameter has been specified. If not, the parameter will be empty. In this case, if the "Character" field has something in it, the handler assumes you want to convert it to the ASCII value. Otherwise, it assumes you want to convert a number to the corresponding character.

if pConversionType is empty then

## figure out which direction to convert:

if field "Character" is not empty then

put "character" into pConversionType

else

put "ASCII" into pConversionType

end if

end if

You can use this method with any handler to assign a default value to one or more of the parameters. Just check whether the parameter is empty. If it is, then no value has been passed, and you set the default value by simply putting the desired default into the parameter

Checking what is to be converted

Once it's figured out what conversion type to use, the handler makes sure there's something in the field to convert. If not, it uses the return control structure to pass an error message back to the calling handler. Since this is a message handler, the returned value is placed in the result function. When using the "convertChar" custom command, a handler can check the result to find out whether there was an error:

on mouseUp

convertChar

if the result is not empty then

beep

end if

end mouseUp

Displaying an ASCII character

Displaying a character's ASCII value is simple: the handler just puts the resulting number into the "ASCII" field. However, displaying an ASCII value's corresponding character is a little more complicated, because some characters (such as the backspace character) aren't printable, that is, they can't be displayed in a field or on the screen. In preparing this example, the button that contains the script was given a custom property called "asciiChart" that contains the standard 3-letter codes for the non-printable characters. (You could also write a custom function that would return the 3-letter code given the ASCII value.) The handler uses this custom property to display these codes. (If you want to accept only printable characters, you can modify the handler so that instead of checking the custom property, it displays an error message if the ASCII value indicates a non-printable character.) If the character is printable, the handler puts it into the "Character" field.

The convertChar command code

on convertChar pConversionType

if pConversionType is empty then

## figure out which direction to convert:

if field "Character" is not empty then

put "character" into pConversionType

else

put "ASCII" into pConversionType

end if

end if

 

if field pConversionType is empty then

return "Error: nothing to convert!"

else

## do the conversion:

if pConversionType is "character" then

## convert character to ASCII:

put charToNum(char 1 of field "Character") into field "ASCII"

else

## convert ASCII value to character:

get field "ASCII"

if (it is not an integer) or (it > 255) or (it < zero) then

## invalid character:

return "Error: ASCII values are between 0 and 255."

put empty into field "Character"

select text of field "ASCII"

else if (it <= 32) or (it = 127) then

## non-printable character:

put item 2 of line it + 1 of the asciiChart of me into field "Character"

else

## ordinary character:

put numToChar(it) into field "Character"

end if

end if

end if

end convertChar

A more general example

It is often convenient, as in this example, to get or change a field's contents directly in a utility handler. (Working with a field is significantly slower than working with a parameter or variable. But in this example, you don't need to refer to the field often enough to really make a difference.) The example could be made more general simply by replacing the field references with a parameter, as in the following fragment:

on convertChar theSource,whatToConvert -- new parameter

if theSource is "character" then

put charToNum(char 1 of whatToConvert) into field "ASCII"

else -- convert ASCII value to character

-- etc. ..

0 Comments

Add your comment

E-Mail me when someone replies to this comment