Q(uick)BASIC Function: ASC

Quick View

ASC Function

A string processing function that returns a numeric value that is the ASCII code for the first character in a string expression

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • ASC(stringexpression$)
  • CHR$(ascii-code%)
Description/Parameter(s)
stringexpression$ Any string expression.
ascii-code% The ASCII code of the desired character.

ASC returns the ASCII code for the first character in a string expression.

CHR$ returns the character corresponding to a specified ASCII code.

Example
PRINT ASC("Q") 'Output is: 81 PRINT CHR$(65) 'Output is: A
Syntax
  • ASC(stringexpression)
Description/Parameter(s)

If stringexpression is null, ASC produces a run-time error message "Illegal function call."

Example

The following example uses ASC to calculate a hash value - an index value for a table or file - from a string:

CONST HASHTABSIZE = 101 CLS 'Clear the screen INPUT "Enter a name: ",Nm$ 'Prompt for input TmpVal = 0 FOR I=1 TO LEN(Nm$) ' Convert the string to a number by summing the values ' of individual letters. TmpVal = TmpVal + ASC(MID$(Nm$,I,1)) NEXT I ' Divide the sum by the size of the table. HashValue = TmpVal MOD HASHTABSIZE PRINT "The hash value is "; HashValue

Sample Output:

Enter a name: Bafflegab The hash value is 66
Syntax
  • ASC(stringexpression$)
Description/Parameter(s)

If the argument stringexpression$ is null, BASIC generates the run-time error message, "Illegal function call."

Example

This example uses the ASC function to calculate a hash value - an index value for a table or file - from a string.

CONST HASHTABSIZE = 101 CLS 'Clear the screen. INPUT "Enter a name: ", Nm$ 'Prompt for input. TmpVal = 0 FOR I = 1 TO LEN(Nm$) 'Convert the string to a number by summing the values 'of individual letters. TmpVal = TmpVal + ASC(MID$(Nm$, I, 1)) NEXT I 'Divide the sum by the size of the table. HashValue = TmpVal MOD HASHTABSIZE PRINT "The hash value is "; HashValue

Sample Output:

Enter a name: Bafflegab The hash value is 66