DAY 014: ASC - Printable Version +- QB64 Phoenix Edition (https://staging.qb64phoenix.com) +-- Forum: Official Links (https://staging.qb64phoenix.com/forumdisplay.php?fid=16) +--- Forum: Learning Resources and Archives (https://staging.qb64phoenix.com/forumdisplay.php?fid=13) +---- Forum: Keyword of the Day! (https://staging.qb64phoenix.com/forumdisplay.php?fid=49) +---- Thread: DAY 014: ASC (/showthread.php?tid=1149) |
DAY 014: ASC - SMcNeill - 11-18-2022 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) RE: DAY 014: ASC - Pete - 11-18-2022 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 Pete RE: DAY 014: ASC - james2464 - 11-18-2022 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. RE: DAY 014: ASC - Pete - 11-18-2022 (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 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 RE: DAY 014: ASC - mnrvovrfc - 11-18-2022 (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 MastergyMaybe 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. RE: DAY 014: ASC - Pete - 11-19-2022 It can't return zero, because zero represents ASC(CHR$(0)) A null string simply cannot be identified by ASC() Pete RE: DAY 014: ASC - Pete - 11-19-2022 Oh, another interesting observation is ASC() should not be used as a true/false indicator. Code: (Select All) $CONSOLE:ONLY Pete RE: DAY 014: ASC - PhilOfPerth - 11-19-2022 If you're not sure how to do it, just asc! RE: DAY 014: ASC - SMcNeill - 11-19-2022 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. |