ReadyBASIC
BASIC Language Reference Manual > Command and Function Reference >

ASC Function

Summary

Converts a character (in a string) into the corresponding Unicode or ASCII code number for that character.

Syntax

ASC(string expression)

Remarks and Examples

Each character that can be displayed has a numeric code associated with it. Traditionally, in most BASIC dialects, the character set has been based on the ASCII standard, where the decimal numbers 32 through 126 are associated with printable characters.

ReadyBASIC uses the Unicode standard, which supports the characters of virtually all languages in common use today. Characters are associated with a number between 0 and 65535 in the Unicode charts. Numbers 32 through 126 remain the same as in ASCII.

The ASC function converts a string containing a character to its corresponding Unicode or ASCII code number.

For example, to find the Unicode or ASCII code number for the lower-case letter "z":

PRINT ASC("z")
122

If the string supplied to the ASC function contains more than one character, the code of only the first character in the string is returned. If an empty string is supplied to the ASC function, zero will be returned.

The following sample program shows how you can use the numeric representations of characters to encrypt and decrypt text using a simple method called the Caesar cypher.

10 REM Encoder
100 INPUT "Enter a string to encode: ", A$
110 SHIFTVALUE% = 3
120 FOR X% = 1 TO LEN(A$)
130 E$ = E$ & CHR$(ASC(MID$(A$, X%, 1)) + SHIFTVALUE%)
140 NEXT X%
150 PRINT "The encoded string is: "; E$
RUN
Enter a string to encode: Hi Bob
The encoded string is: Kl#Ere
10 REM Decoder
100 INPUT "Enter a string to decode: ", A$
110 SHIFTVALUE% = 3
120 FOR X% = 1 TO LEN(A$)
130 E$ = E$ & CHR$(ASC(MID$(A$, X%, 1)) - SHIFTVALUE%)
140 NEXT X%
150 PRINT "The decoded string is: "; E$
RUN
Enter a string to decode: Kl#Ere
The decoded string is: Hi Bob

To convert a numeric ASCII or Unicode code number into a string representation of the corresponding character, use the CHR$ function.

Note: While ReadyBASIC supports the Unicode standard, the Courier New font presently used for the console display may not support all characters in the Unicode palette.

TODO: Insert or link to basic ASCII table. Link to Unicode charts (off-site)




Copyright 2006-2008, Kevin Matz, All Rights Reserved.