CHR$: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
Tags: Undo Reverted
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 8: Line 8:
{{PageDescription}}
{{PageDescription}}
* Valid ASCII {{Parameter|code%}} numbers range from 0 to 255.
* Valid ASCII {{Parameter|code%}} numbers range from 0 to 255.
* The character code of a character can be found using [[ASC]].
* The character code of a character can be found using the [[ASC (function)]].
* Some control codes below 32 will not [[PRINT]] or will move the screen cursor, unless [[_CONTROLCHR|_CONTROLCHR OFF]] is used.
* Some control codes below 32 will not [[PRINT]] or will move the screen cursor, unless [[_CONTROLCHR|_CONTROLCHR OFF]] is used.


Line 37: Line 37:




''Example 3:'' Using [[ASC]] and [[CHR$]] to ''encrypt'' a text file size up to 32K bytes
''Example 3:'' Using [[ASC (function)|ASC]] and [[CHR$]] to ''encrypt'' a text file size up to 32K bytes
{{CodeStart}}{{Cl|OPEN}} FileName$ {{Cl|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #1 ' FileName to be encrypted
{{CodeStart}}{{Cl|OPEN}} FileName$ {{Cl|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #1 ' FileName to be encrypted
{{Cl|IF...THEN|IF}} {{Cl|LOF}}(1) <= 32000 {{Cl|THEN}} Text$ = {{Cl|INPUT$}}({{Cl|LOF}}(1), 1) ' get Text as one string
{{Cl|IF...THEN|IF}} {{Cl|LOF}}(1) <= 32000 {{Cl|THEN}} Text$ = {{Cl|INPUT$}}({{Cl|LOF}}(1), 1) ' get Text as one string
Line 43: Line 43:
Send$ = "" ' clear value
Send$ = "" ' clear value
{{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} {{Cl|LEN}}(Text$)
{{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} {{Cl|LEN}}(Text$)
     Letter$ = {{Cl|MID$}}(Text$, i, 1) ' get each character in the text
     Letter$ = {{Cl|MID$ (function)|MID$}}(Text$, i, 1) ' get each character in the text
     Code = {{Cl|ASC}}(Letter$)
     Code = {{Cl|ASC (function)|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}}
     {{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
         Letter$ = {{Cl|CHR$}}(Code + 130) ' change letter's ASCII character by 130
Line 57: Line 57:




''Example 4:'' '''Decrypting''' the above encrypted text file (32K byte file size limit).  
''Example 4:'' '''Decrypting''' the above encrypted text file (32K byte file size limit).
{{CodeStart}}{{Cl|OPEN}} FileName$ {{Cl|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #1      ' FileName to be decrypted
{{CodeStart}}{{Cl|OPEN}} FileName$ {{Cl|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #1      ' FileName to be decrypted
     Text$ = {{Cl|INPUT$}}({{Cl|LOF}}(1), 1)        ' open Text as one string
     Text$ = {{Cl|INPUT$}}({{Cl|LOF}}(1), 1)        ' open Text as one string
Line 63: Line 63:
Send$ = ""
Send$ = ""
{{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} {{Cl|LEN}}(Text$)
{{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} {{Cl|LEN}}(Text$)
     Letter$ = {{Cl|MID$}}(Text$, i, 1)
     Letter$ = {{Cl|MID$ (function)|MID$}}(Text$, i, 1)
     Code = {{Cl|ASC}}(Letter$)
     Code = {{Cl|ASC (function)|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}}
     {{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
         Letter$ = {{Cl|CHR$}}(Code - 130)  ' change back to a Letter character
Line 72: Line 72:
{{Cl|OPEN}} FileName$ {{Cl|FOR}} {{Cl|OUTPUT}} {{Cl|AS}} #1 ' Erase file for decrypted text
{{Cl|OPEN}} FileName$ {{Cl|FOR}} {{Cl|OUTPUT}} {{Cl|AS}} #1 ' Erase file for decrypted text
     {{Cl|PRINT (file statement)|PRINT}} #1, Send$ ' place Text as one string
     {{Cl|PRINT (file statement)|PRINT}} #1, Send$ ' place Text as one string
{{Cl|CLOSE}} #1 '' ''
{{Cl|CLOSE}} #1
{{CodeEnd}}
{{CodeEnd}}
{{small|Code by Ted Weissgerber}}
{{Small|Code by Ted Weissgerber}}
:''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.
:''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.




{{PageSeeAlso}}
{{PageSeeAlso}}
* [[ASC]], [[ASC (statement)]]
* [[ASC]], [[ASC (function)]]
* [[INKEY$]]
* [[INKEY$]]
* [[ASCII|ASCII character codes]]
* [[ASCII|ASCII character codes]]

Latest revision as of 00:30, 26 February 2023

The CHR$ function returns the character associated with a certain character code as a STRING.


Syntax

result$ = CHR$(code%)


Description

  • Valid ASCII code% numbers range from 0 to 255.
  • The character code of a character can be found using the ASC (function).
  • Some control codes below 32 will not PRINT or will move the screen cursor, unless _CONTROLCHR OFF is used.


Examples

Example 1: Outputs the characters of several character codes:

PRINT CHR$(65); CHR$(65 + 32)
PRINT CHR$(66); CHR$(66 + 32)
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"


Example 2: To cut down on typing CHR$(???) all day, define often used characters as variables such as Q$ = CHR$(34) as shown.


DIM Q AS STRING * 1   'define as one byte string(get rid of $ type suffix too)
Q = CHR$(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

OPEN FileName$ FOR INPUT AS #1 ' FileName to be encrypted
IF LOF(1) <= 32000 THEN Text$ = INPUT$(LOF(1), 1) ' get Text as one string
CLOSE #1
Send$ = "" ' clear value
FOR i = 1 TO LEN(Text$)
    Letter$ = MID$(Text$, i, 1) ' get each character in the text
    Code = ASC(Letter$)
    IF (Code > 64 AND Code < 91) OR (Code > 96 AND Code < 123) THEN
        Letter$ = CHR$(Code + 130) ' change letter's ASCII character by 130
    END IF
    Send$ = Send$ + Letter$ ' reassemble string with just letters encrypted
NEXT i
OPEN FileName$ FOR OUTPUT AS #1 ' erase FileName to be encrypted
PRINT #1, Send$   ' Text as one string
CLOSE #1
Warning: The routine above will change an original text file to be unreadable. Use a second file name to preserve the original file.


Example 4: Decrypting the above encrypted text file (32K byte file size limit).

OPEN FileName$ FOR INPUT AS #1       ' FileName to be decrypted
    Text$ = INPUT$(LOF(1), 1)         ' open Text as one string
CLOSE #1
Send$ = ""
FOR i = 1 TO LEN(Text$)
    Letter$ = MID$(Text$, i, 1)
    Code = ASC(Letter$)
    IF (Code > 194 AND Code < 221) OR (Code > 226 AND Code < 253) THEN
        Letter$ = CHR$(Code - 130)  ' change back to a Letter character
    END IF
    Send$ = Send$ + Letter$ ' reassemble string as normal letters
    NEXT i
OPEN FileName$ FOR OUTPUT AS #1 ' Erase file for decrypted text
    PRINT #1, Send$ ' place Text as one string
CLOSE #1
Code by Ted Weissgerber
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.


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link