
I like working in the “lower case” font on the Commodores. That way a SHIFT creates a capital letter, all other letters are lower case, it looks a little better and feels more like using a modern computer (at least to me). When I print out a string, I may from time to time want it to be shown in all uppercase letters though, and there’s an easy conversion trick we can use: OR 128
To apply it, we read out the ASC value of a character and call OR 128 on it (essentially flipping a bit). Call it again and we’ll reverse it back to lower case.
Consider the following code that generates the above screenshot:
10 a$="this is all lower case"
20 for i=1 to len(a$)
30 b$=mid$(a$,i,1)
40 b=asc(b$) or 128
50 c$=c$+chr$(b)
60 next
70 print a$:print c$
Here we loop through each letter of the given variable (b$ is each single letter of a$), grabbing its ASC value (b), then re-assembling this with OR 128 applied in c$. This could be condensed into a one-liner, but it makes the code difficult to read. For example,
print chr$(asc("x") or 128)
will print an upper case letter X from the given lower case x.
Note that this conversion is quite expensive, so the less we convert the snappier our programme will feel.
Mixed Conversions
The above works well for strings that are all upper or all lower case, but what if we had an input that has a mixed scenario? The Livingroom for example. That would just reverse the cases rather than make them consistently upper or lower case.
To solve this, we’ll need a little more logic. First we’ll step through each letter of the string and determine what to do with them. We can figure this out by looking at the ASC values per letter.
- lower case letters are between 65 (a) and 90 (z)
- upper case letters are between 193 (A) and 218 (Z)
Equipped with this knowledge we can device two subroutines and handle each letter type accordingly. Here I convert the lower case letters to upper case, and leave the upper case letters alone:
10 a$="This is a Mixed Case Scenario"
20 for i=1 to len(a$)
30 b$=mid$((a$),i,1)
35 b=asc(b$)
40 if b>=193 and b<=218 then gosub 100:rem handle upper case 45 if b>=65 and b<=90 then gosub 200:rem handle lower case
50 c$=c$+chr$(b)
60 next
70 print:print a$:print c$
100 rem upper case letters
199 return
200 rem lower case letters
210 b=b or 128
299 return
