-
Notifications
You must be signed in to change notification settings - Fork 0
CHR$
The CHR$ function returns the character associated with a certain character code as a STRING.
- = CHR$()
- Valid ASCII numbers range from 0 to 255.
- The character code of a character can be found using ASC.
- Some control codes below 32 will not PRINT or will move the screen cursor, unless _CONTROLCHR OFF is used.
Aa Bb
- Explanation: 65 is the ASCII code for "A" and 65 + 32 is the ASCII code for "a". 66 is the ASCII code for "B" and 66 + 32 is the ASCII code for "b"
Q AS * 1 'define as one byte string(get rid of $ type suffix too) Q = (34) 'Q will now represent the elusive quotation mark in a string
PRINT "This text uses "; Q; "quotation marks"; Q; " that could have caused a syntax error!"
This text uses "quotation marks" that could have caused a syntax error!
Example 3: Using ASC and CHR$ to encrypt a text file size up to 32K bytes FileName$ #1 ' FileName to be encrypted (1) <= 32000 Text$ = ((1), 1) ' get Text as one string #1 Send$ = "" ' clear value i = 1 (Text$)
Letter$ = {{Cl|MID$}}(Text$, i, 1) ' get each character in the text
Code = {{Cl|ASC}}(Letter$)
{{Cl|IF...THEN|IF}} (Code > 64 {{Cl|AND (boolean)|AND}} Code < 91) {{Cl|OR (boolean)|OR}} (Code > 96 {{Cl|AND (boolean)|AND}} Code < 123) {{Cl|THEN}}
Letter$ = {{Cl|CHR$}}(Code + 130) ' change letter's ASCII character by 130
{{Cl|END IF}}
Send$ = Send$ + Letter$ ' reassemble string with just letters encrypted
i FileName$ #1 ' erase FileName to be encrypted #1, Send$ ' Text as one string #1
- Warning: The routine above will change an original text file to be unreadable. Use a second file name to preserve the original file.
Text$ = {{Cl|INPUT$}}({{Cl|LOF}}(1), 1) ' open Text as one string
#1 Send$ = "" i = 1 (Text$)
Letter$ = {{Cl|MID$}}(Text$, i, 1)
Code = {{Cl|ASC}}(Letter$)
{{Cl|IF...THEN|IF}} (Code > 194 {{Cl|AND (boolean)|AND}} Code < 221) {{Cl|OR (boolean)|OR}} (Code > 226 {{Cl|AND (boolean)|AND}} Code < 253) {{Cl|THEN}}
Letter$ = {{Cl|CHR$}}(Code - 130) ' change back to a Letter character
{{Cl|END IF}}
Send$ = Send$ + Letter$ ' reassemble string as normal letters
{{Cl|NEXT}} i
FileName$ #1 ' Erase file for decrypted text
{{Cl|PRINT (file statement)|PRINT}} #1, Send$ ' place Text as one string
#1
- Explanation: Examples 3 and 4 encrypt and decrypt a file up to 32 thousand bytes. INPUT$ can only get strings less than 32767 characters. The upper and lower case letter characters are the only ones altered, but the encryption and decryption rely on the fact that most text files do not use the code characters above 193. You could alter any character from ASCII 32 to 125 without problems using the 130 adder. No ASCII code above 255 is allowed. Don't alter the codes below code 32 as they are control characters. Specifically, characters 13 and 10 (CrLf) may be used for line returns in text files.