DAY 014: ASC
#1
A command that's been around since the beginning of the language -- but also one that has expanded and evolved to make it much more flexible and powerful for modern programmers.

What is it?  ASC is a simple little command which lets us to either set/get the ASCII value to/from a string character.

How do we use it?  

To get a value, it's a simple function call like: value = ASC(a$, position)
To set a value, it's a simple sub call such as:  ASC(a$, position) = 97

A simple example to showcase these methods a little:

Code: (Select All)
Screen _NewImage(800, 600, 32)

a$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

'Back in the days of QB45, this is how folks made use of ASC:
For i = 1 To Len(a$)
    temp$ = Mid$(a$, i, 1)
    Print temp$; " = "; Asc(temp$)
Next
Sleep

'Then, along came QB64 with its first improvement!
'the ability to specify which byte we wanted!
For i = 1 To Len(a$)
    Locate i, 20
    Print Mid$(a$, i, 1); " = "; Asc(a$, i)
Next
Sleep

'Did you notice that we didn't actually need to use MID$ to get a single character anymore?
'Instead, we just specified which character we wanted to get the ASC value of, out of our string.

'And, as if that improvement wasn't enough, a SUB ASC was written for QB64!
temp$ = a$ 'a perfect match
For i = 1 To Len(a$)
    Asc(temp$, i) = Asc(a$, Len(a$) - i + 1)
Next
Print
Print temp$
Sleep

'Notice how we just used ASC to assign characters to our string, based entirely off their ASCII value?

Cls
Print "ASC";
_Delay .5: Print ".";
_Delay .5: Print ".";
_Delay .5: Print "."
Print "Not the same old command that cavemen used to use when programming!"
Print
Print "You can now specify which byte you want the ASCII value of..."
Print "and you can now assign characters to a string with just their ASCII value."
Reply
#2
Marty McFly would beg to differ, calling it the keyword of last month, but back to the present...

This is one I sometimes use if I want to compare a string by it's numerical value, mostly for simplicity sake but also if the program is mostly using numerical values and then interacts with strings where a numerical value of some character in the string is required. Yes, ASC() works with MID$() but if MID$ is not used, ASC() simply converts to a numeric value the first character of the string. The only exception is a null string, which will throw an error.

Examples.
Code: (Select All)
WIDTH 80, 42
_CONTROLCHR OFF
FOR i = 1 TO 255
    x$ = CHR$(i)
    SELECT CASE x$
        CASE "a" TO "z"
            PRINT i; x$
    END SELECT
NEXT
SLEEP
CLS
x = ASC("a") - 1
FOR j = 1 TO 26
    PRINT j; CHR$(j + x)
NEXT

x$ = "Pete" ' ASC() will read the first character: "P"
IF ASC(x$) < 127 THEN PRINT "Okay!" ' It can be used to identify a range like 0 - 126.

SLEEP
x$ = "" ' ASC will not accept a null string.
PRINT ASC(x$) ' This will error out.


Pete
If eggs are brain food, Biden takes his scrambled.
Reply
#3
I noticed this keyword for the first time yesterday while taking a look at a program by Mastergy

_Keydown(Asc("w")) 

Instead of using the number for the w key.
Reply
#4
(11-18-2022, 07:19 PM)james2464 Wrote: I noticed this keyword for the first time yesterday while taking a look at a program by Mastergy

_Keydown(Asc("w")) 

Instead of using the number for the w key.

Great example! I almost always go old school with INKEY$, but when I do _KEYDOWN or _KEYHIT the numbers are a _BEOTCH to remember. Using ASC() helps ease that difficulty. Hell, I knew the alphabet by 3rd year college, my freshman year.

Pete
Reply
#5
(11-18-2022, 07:19 PM)james2464 Wrote: I noticed this keyword for the first time yesterday while taking a look at a program by Mastergy

_Keydown(Asc("w")) 

Instead of using the number for the w key.
Maybe not for a letter or digit key but how about an arrow key or a strange key that could be seen easily in a keyboard today but wasn't on the original IBM PC keyboard? Pure expert programmer for those that memorize those "VK" codes!


(11-18-2022, 06:49 PM)Pete Wrote: ... Yes, ASC() works with MID$() but if MID$ is not used, ASC() simply converts to a numeric value the first character of the string. The only exception is a null string, which will throw an error.
This is something that I can't explain and even more after there became such a thing as "ASC() statement". I wish on empty string "ASC()" function returned zero, without interrupting program flow. I'm crabbing now because I should have done it to whoever inside M$ was responsible for QuickBASIC and not daring to change it.
Reply
#6
It can't return zero, because zero represents ASC(CHR$(0))

A null string simply cannot be identified by ASC()

Pete
If eggs are brain food, Biden takes his scrambled.
Reply
#7
Oh, another interesting observation is ASC() should not be used as a true/false indicator.

Code: (Select All)
$CONSOLE:ONLY
_CONTROLCHR OFF
FOR i = 0 TO 255
    PRINT i; CHR$(i);
    IF ASC(CHR$(i)) THEN PRINT "I'm a character!" ELSE PRINT "I'm not a character" ' Note, it misses CHR$(0), which is a blank character.
NEXT

Pete
If eggs are brain food, Biden takes his scrambled.
Reply
#8
If you're not sure how to do it, just asc! Big Grin
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Reply
#9
As yesterday's tomorrow is now today, I've updated the first post with an example and brief explanation for the ASC keyword.  (The only reason I put a placeholder up early was so I didn't forget which keyword you guys were wanting me to do for the day.  LOL!)

Our Keyword of The Day is up and now available, TODAY.  Wink
Reply




Users browsing this thread: 7 Guest(s)