StringTools - Maple Programming Help

Home : Support : Online Help : Programming : Names and Strings : StringTools Package : Encodings : StringTools/Encode

StringTools

 Encode
 encode a string using a specified encoding
 Decode
 decode an encoded string

 Calling Sequence Encode( s, 'encoding' = enc ) Decode( s, 'encoding' = enc )

Parameters

 s - string; any Maple string enc - (optional) name of the encoding to use (one of: "null", "base64", "rot13", 'rot'[n], 'alpharot'[n], 'percent' where n is an integer in the range $0..255$)

Description

 • The procedure Encode takes a string s, and an optional encoding= argument, and returns a string obtained from s by applying the encoding method indicated by the encoding= option.
 • The procedure Decode takes an encoded string s, and an optional encoding= argument, and returns the string obtained by decoding according to the method indicated in the second argument. It is an approximate inverse of Encode.
 • The currently supported encodings are:

 null the null encoding (does nothing) rot13 classical Caesar cypher on alphabetic characters rot[n] classical Caesar cypher on nonzero bytes alpharot[n] classical Caesar cypher on alphabetic (letter) characters base64 base 64 encoding as described in RFC 2045. percent percent encoding for URLs
 • The default encoding is null, which returns the input unchanged.
 • The rot13 encoding is a simple Caesar cypher that is applied only to alphabetic characters in the input string. Other characters are not encoded.) It treats the alphabetic characters ("a".."z" and "A".."Z") as integers modulo 26 and shifts each one by 13.
 • The general ${\mathrm{rot}}_{n}$ encoding performs a Caesar cypher modulo 256 on the individual bytes in the input string by treating each as an integer in the range $1..255$ and shifting each by the offset n modulo 256. The parameter n of the encoding must be an integer in the range $0..255$.
 • While the general ${\mathrm{rot}}_{n}$ encoding is applied indiscriminately to all nonzero bytes of a string, the ${\mathrm{alpharot}}_{n}$ encoding is generally more suitable for didactic purposes. The parameter n may be any integer in the range $0..25$, and only alphabetic characters in the text are affected by the encoding. Other characters in the text are passed through to the output unchanged. Upper and lowercase letters are encoded independently, each letter being shifted cyclically (modulo 26) within its own case.
 • The base64 encoding is described in detail in RFC 2045. It is standard encoding for arbitrary data in a subset of the US-ASCII character set so that it can be transmitted via channels that are unable to handle 8-bit character data. It is frequently used in electronic mail (SMTP) and web (HTTP) transactions.
 • The percent encoding is used to encode URLs. Certain characters in a URL are replaced with a % sign, followed by a two-digit hexadecimal code for the US-ASCII code point of the character.
 • All of the StringTools package commands treat strings as (null-terminated) sequences of $8$-bit (ASCII) characters.  Thus, there is no support for multibyte character encodings, such as unicode encodings.

Examples

 > $\mathrm{with}\left(\mathrm{StringTools}\right):$
 > $\mathrm{Encode}\left("a string"\right)$
 ${"a string"}$ (1)
 > $\mathrm{Decode}\left("a string"\right)$
 ${"a string"}$ (2)
 > $e≔\mathrm{Encode}\left("a string",'\mathrm{encoding}'='\mathrm{base64}'\right)$
 ${e}{≔}{"YSBzdHJpbmc="}$ (3)
 > $\mathrm{Decode}\left(e,'\mathrm{encoding}'='\mathrm{base64}'\right)$
 ${"a string"}$ (4)
 > $\mathrm{rot13}≔\mathrm{rcurry}\left(\mathrm{Encode},'\mathrm{encoding}'="rot13"\right):$
 > $s≔\mathrm{rot13}\left("abc"\right)$
 ${s}{≔}{"nop"}$ (5)
 > $\mathrm{rot13}\left(s\right)$
 ${"abc"}$ (6)
 > $s≔\mathrm{Encode}\left("abc",'\mathrm{encoding}'='\mathrm{rot}'\left[2\right]\right)$
 ${s}{≔}{"cde"}$ (7)
 > $\mathrm{Decode}\left(s,'\mathrm{encoding}'='\mathrm{rot}'\left[2\right]\right)$
 ${"abc"}$ (8)
 > $s≔\mathrm{Encode}\left("abc123DEF",'\mathrm{encoding}'='\mathrm{alpharot}'\left[25\right]\right)$
 ${s}{≔}{"zab123CDE"}$ (9)
 > $\mathrm{Decode}\left(s,'\mathrm{encoding}'='\mathrm{rot}'\left[25\right]\right)$
 ${"aHI*+,"}$ (10)

References

 Information Sciences Institute, "RFC 2045 Internet Message Bodies," ISI Home Page, http://www.isi.edu/in-notes/rfc2045.txt; accessed 28 November 2005.