coded ← ##.base64 plain         ⍝ Base64 encoding and decoding as used in MIME.
plain ← ##.base64 coded

From Jay Foad, this function en/decodes between plain numbers and base64  encod-
ing as used by MIME format.

References:

http://en.wikipedia.org/wiki/Base64
http://tools.ietf.org/html/rfc2045

Examples:

    base64 0 1 2 3 4 253 254 255            ⍝ encode some octets
AAECAwT9/v8=

    base64 'AAECAwT9/v8='
0 1 2 3 4 253 254 255

    base64 ⎕ucs 'If I were only dafter'     ⍝ encode some ASCII text
SWYgSSB3ZXJlIG9ubHkgZGFmdGVy

    ⎕ucs base64 'SWYgSSB3ZXJlIG9ubHkgZGFmdGVy'
If I were only dafter

    base64 'UTF-8' ⎕ucs '+/⍵÷⍴⍵'            ⍝ encode some UTF-8 text
Ky/ijbXDt+KNtOKNtQ==

    'UTF-8' ⎕ucs base64 'Ky/ijbXDt+KNtOKNtQ=='
+/⍵÷⍴⍵

    80 wrap Hobbes      ⍝ Example from Wikipedia article.
Man is distinguished, not only by his reason, but by this singular passion from
other animals, which is a lust of the mind, that by a perseverance of delight in
the continued and indefatigable generation of knowledge, exceeds the short
vehemence of any carnal pleasure.

    80 wrap base64 ⎕ucs Hobbes
TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNp
bmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1p
bmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBp
bmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhl
bWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=

    tails ← (0 to ¯4) ↓¨ ⊂¯20↑Hobbes

    ↑ tails
any carnal pleasure.
any carnal pleasure
any carnal pleasur
any carnal pleasu
any carnal pleas

    ↑base64∘⎕ucs¨tails
YW55IGNhcm5hbCBwbGVhc3VyZS4=
YW55IGNhcm5hbCBwbGVhc3VyZQ==
YW55IGNhcm5hbCBwbGVhc3Vy
YW55IGNhcm5hbCBwbGVhc3U=
YW55IGNhcm5hbCBwbGVhcw==

See also: hex Data_compression

Back to: contents

Back to: Workspaces