ROT-13 Encoding/Decoding
Posted: Mon Feb 23, 2004 9:51 pm
Code updated for 5.20+
See code comments for description.
See code comments for description.
Code: Select all
; Rotate 13 encoder/decoder function
;
; Some newsgroup postings are rot-13 encoded.
; It is sometimes used to hide possibly offensive material
; from accidental viewing (off-color jokes, for instance).
; The message will appear as gibberish.
;
; Rot-13 encoding can be used for other purposes as well.
; Discussions that give away the ending of a novel or a movie,
; For example, can be encoded so that only someone so inclined
; may view it.
Procedure.s Rot13(text.s)
Protected result.s, textLen
textLen = Len(Text)
If textLen > 0
result = Space(textLen)
Protected i, c.w
For i = 0 To (textLen - 1)
c = PeekB(@text + i)
; If A..Z add 13 If then > Z subtract 26
; in order to wrap it back into range
If (c >=65) And (c <= 90)
c + 13
If (c > 90) : c - 26 : EndIf
Else
; If a..z add 13 If then > z subtract 26
; in order to wrap it back into range
If (c >= 97) And (c <= 122)
c + 13
If (c > 122) : c - 26 : EndIf
Else
; Otherwise do nothing
EndIf
EndIf
PokeB(@result + i, c)
Next i
EndIf
ProcedureReturn result
EndProcedure
If OpenConsole()
s.s = "Dude - where's my car!"
PrintN("Original Text: " + s)
s = Rot13(s)
PrintN("Rot-13 Encoded: " + s)
s = Rot13(s)
PrintN("Rot-13 Decoded: " + s)
PrintN("")
PrintN("-- The alphabet")
s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
PrintN("Original Text: " + s)
s = Rot13(s)
PrintN("Rot-13 Encoded: " + s)
s = Rot13(s)
PrintN("Rot-13 Decoded: " + s)
Print("Press a key when done... ")
Repeat : Until Inkey()
CloseConsole()
EndIf
End