Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.
ASC()
, CHR()
This pair of functions converts between numbers and the ASCII character set. ASC(a character) returns the ASCII code number for that character, while CHR(a number) returns the character whose ASCII number you pass. Note that ASC()
can take a whole character string as a parameter, but it returns the ASCII code of the first character in the string.
nNumber = ASC( cString )
cChar = CHR( nNumber )
Since the ASCII character set has 256 items in it, numbered from 0 to 255, that’s the range of these functions. CHR()
chokes if you hand it a number less than 0 or greater than 255.
Be aware that the appearance of the results of CHR()
depends on the display font. Most Windows fonts don’t use the ASCII character set. You’re generally pretty safe with the numbers that correspond to the English alphabet, punctuation and digits (though some fonts, like Wingdings, mess with those, too), but outside that range, especially above character 127, you’ll see very different results as you change fonts.
CHR()
was much more useful before FoxPro gained the capability to refer to many of the special characters using the curly brace notation. We used to write things like this:
KEYBOARD CHR(13)
But it’s much more readable as:
KEYBOARD "{ENTER}"
so we switched.
ASC()
is a handy hacker’s tool for use in dissecting binary data. ASC()
gives you the ability to convert a single character to numeric format. You can then use that number to perform binary math, splice and dice bits for calculations like timestamps, or add it to other numbers for multi-byte numeric conversion.
While ASC()
is valuable if the character is unknown, we don’t use ASC()
to check expected values. It’s handy once in a while when you want to check a string for a special character that you can’t write in curly brace notation, but it’s just as easy to write:
IF LEFT(cString,1)=CHR(<some value>)
as
IF ASC(cString)=<some value>
and somehow, the first form seems more readable, so ASC()
doesn’t get much use.