KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: ROT13 encoding and decoding in 4D
PRODUCT: 4D | VERSION: 13.2 | PLATFORM: Mac & Win
Published On: February 8, 2013

The following method is an implementation of the ROT13 simple cipher in 4D:

// ROT13 encoding takes a string and rotates the alphabet 13 letters,
// the same method can be used for both encoding and decoding ROT13

C_TEXT($1;$input_t) // input string - to be ROT13 encoded
C_TEXT($0;$output_t) // output string - encoded in ROT13
C_LONGINT($char_l) // character code used for rotation
C_LONGINT($len) // length of string
C_LONGINT($a) // loop iteration counter
C_TEXT($tmp) // temp var for chars

Case of
 :(Count parameters=2)
   $char_l:=Character code($1)
   $output_t:=Char($char_l)
   If (($char_l>64) & ($char_l<78))
      $output_t:=(Char($char_l+13))
   End if
   If (($char_l>77) & ($char_l<91))
      $output_t:=(Char($char_l-13))
   End if
   If (($char_l>96) & ($char_l<110))
      $output_t:=(Char($char_l+13))
   End if
   If (($char_l>109) & ($char_l<123))
      $output_t:=(Char($char_l-13))
   End if
 :(Count parameters=1)
   $input_t:=$1
   $len:=Length($input_t)
   $output_t:=""
   For ($a;1;$len)
      EXECUTE METHOD(Current method name;$tmp;Substring($input_t;$a;1);"rotate")
      $output_t:=$output_t+$tmp
   End for
End case
$0:=$output_t


If the method is saved as UTIL_rot13 is can be used like this:
$rotText:=UTIL_rot13("This is some sample text")


In the above example $rotText would be equal to: "Guvf vf fbzr fnzcyr grkg"

Converting this back with the same method would be done like this:
$rotText:=UTIL_rot13("Guvf vf fbzr fnzcyr grkg")