Asc ( string$ )
Parameters
| string - the string to look at; only its first character is used |
Description
|
Returns the character code of the first character of a string. Asc( "A" ) is 65. It is the exact opposite of Chr, which turns a code back into a character. Codes come back in the range 0 to 255. That makes Asc the natural way to work with text as numbers: checking whether a typed character is a digit, converting a letter to an index into a font sheet, or building a simple checksum of a name. An empty string returns -1, which is worth testing for when the string came from user input or a file - a blank line will otherwise sail through as a valid-looking negative code. Asc only ever looks at the first character. To walk a whole string, pull each character out with Mid and pass that in, using Len to bound the loop. See also: Chr, Mid, Len, GetKey. |
Example
; Asc Example ; ----------- ; Asc returns the ASCII code of the first character of a string. ; Character codes are handy for checksums, sorting and name entry. initials$="ACE" Print "High-score initials: "+initials$ Print "" ; Show the code of each letter For i=1 To Len(initials$) letter$=Mid$(initials$,i,1) Print "Asc('"+letter$+"') = "+Asc(letter$) Next ; Letters can be compared as numbers - that is how names get sorted Print "" Print "'A' sorts before 'B' because "+Asc("A")+" < "+Asc("B") ; A simple save-file checksum built from the character codes checksum=0 For i=1 To Len(initials$) checksum=checksum+Asc(Mid$(initials$,i,1)) Next Print "Checksum of "+initials$+" = "+checksum Print "" Print "Press any key to close the example" WaitKey End
Index