Base64 encode data: Difference between revisions

Content added Content deleted
(added RPL)
(Add ed example)
 
Line 675: Line 675:
end;
end;
end.</syntaxhighlight>
end.</syntaxhighlight>

=={{header|Ed}}==

See the sister algo [[Base64_decode_data#Ed]] for implementation details/quirks.

<syntaxhighlight>
H
,p
g/(.)/s// \1 /g
# text -> binary
g/[ ]![ ]/s//00100001/g
g/[ ]"[ ]/s//00100010/g
g/[ ]#[ ]/s//00100011/g
g/[ ]$[ ]/s//00100100/g
g/[ ]%[ ]/s//00100101/g
g/[ ]\&[ ]/s//00100110/g
g/[ ]'[ ]/s//00100111/g
g/[ ]\([ ]/s//00101000/g
g/[ ]\)[ ]/s//00101001/g
g/[ ]\*[ ]/s//00101010/g
g/[ ]\+[ ]/s//00101011/g
g/[ ],[ ]/s//00101100/g
g/[ ]-[ ]/s//00101101/g
g/[ ]\.[ ]/s//00101110/g
g/[ ]/[ ]/s//00101111/g
g/[ ]0[ ]/s//00110000/g
g/[ ]1[ ]/s//00110001/g
g/[ ]2[ ]/s//00110010/g
g/[ ]3[ ]/s//00110011/g
g/[ ]4[ ]/s//00110100/g
g/[ ]5[ ]/s//00110101/g
g/[ ]6[ ]/s//00110110/g
g/[ ]7[ ]/s//00110111/g
g/[ ]8[ ]/s//00111000/g
g/[ ]9[ ]/s//00111001/g
g/[ ]:[ ]/s//00111010/g
g/[ ];[ ]/s//00111011/g
g/[ ]<[ ]/s//00111100/g
g/[ ]=[ ]/s//00111101/g
g/[ ]>[ ]/s//00111110/g
g/[ ]\?[ ]/s//00111111/g
g/[ ]@[ ]/s//01000000/g
g/[ ]A[ ]/s//01000001/g
g/[ ]B[ ]/s//01000010/g
g/[ ]C[ ]/s//01000011/g
g/[ ]D[ ]/s//01000100/g
g/[ ]E[ ]/s//01000101/g
g/[ ]F[ ]/s//01000110/g
g/[ ]G[ ]/s//01000111/g
g/[ ]H[ ]/s//01001000/g
g/[ ]I[ ]/s//01001001/g
g/[ ]J[ ]/s//01001010/g
g/[ ]K[ ]/s//01001011/g
g/[ ]L[ ]/s//01001100/g
g/[ ]M[ ]/s//01001101/g
g/[ ]N[ ]/s//01001110/g
g/[ ]O[ ]/s//01001111/g
g/[ ]P[ ]/s//01010000/g
g/[ ]Q[ ]/s//01010001/g
g/[ ]R[ ]/s//01010010/g
g/[ ]S[ ]/s//01010011/g
g/[ ]T[ ]/s//01010100/g
g/[ ]U[ ]/s//01010101/g
g/[ ]V[ ]/s//01010110/g
g/[ ]W[ ]/s//01010111/g
g/[ ]X[ ]/s//01011000/g
g/[ ]Y[ ]/s//01011001/g
g/[ ]Z[ ]/s//01011010/g
g/[ ]\[[ ]/s//01011011/g
g/[ ]\\[ ]/s//01011100/g
g/[ ]\][ ]/s//01011101/g
g/[ ]\^[ ]/s//01011110/g
g/[ ]_[ ]/s//01011111/g
g/[ ]`[ ]/s//01100000/g
g/[ ]a[ ]/s//01100001/g
g/[ ]b[ ]/s//01100010/g
g/[ ]c[ ]/s//01100011/g
g/[ ]d[ ]/s//01100100/g
g/[ ]e[ ]/s//01100101/g
g/[ ]f[ ]/s//01100110/g
g/[ ]g[ ]/s//01100111/g
g/[ ]h[ ]/s//01101000/g
g/[ ]i[ ]/s//01101001/g
g/[ ]j[ ]/s//01101010/g
g/[ ]k[ ]/s//01101011/g
g/[ ]l[ ]/s//01101100/g
g/[ ]m[ ]/s//01101101/g
g/[ ]n[ ]/s//01101110/g
g/[ ]o[ ]/s//01101111/g
g/[ ]p[ ]/s//01110000/g
g/[ ]q[ ]/s//01110001/g
g/[ ]r[ ]/s//01110010/g
g/[ ]s[ ]/s//01110011/g
g/[ ]t[ ]/s//01110100/g
g/[ ]u[ ]/s//01110101/g
g/[ ]v[ ]/s//01110110/g
g/[ ]w[ ]/s//01110111/g
g/[ ]x[ ]/s//01111000/g
g/[ ]y[ ]/s//01111001/g
g/[ ]z[ ]/s//01111010/g
g/[ ]\{[ ]/s//01111011/g
g/[ ]\|[ ]/s//01111100/g
g/[ ]\}[ ]/s//01111101/g
g/[ ]~[ ]/s//01111110/g
# Two special cases last so that they don't interfere with the rest of whitespace-separated conversion
g/[ ][ ][ ]/s//00100000/g
g/$/s//00001010/g
,j
# split into blocks of six
g/[01]{6}/s// & /g
g/\b[01]{1}\b/s//&0/g
g/\b[01]{2}\b/s//&0/g
g/\b[01]{3}\b/s//&0/g
g/\b[01]{4}\b/s//&0/g
g/\b[01]{5}\b/s//&0/g
# And encode the combinations to base64
g/000000/s//A/g
g/000001/s//B/g
g/000010/s//C/g
g/000011/s//D/g
g/000100/s//E/g
g/000101/s//F/g
g/000110/s//G/g
g/000111/s//H/g
g/001000/s//I/g
g/001001/s//J/g
g/001010/s//K/g
g/001011/s//L/g
g/001100/s//M/g
g/001101/s//N/g
g/001110/s//O/g
g/001111/s//P/g
g/010000/s//Q/g
g/010001/s//R/g
g/010010/s//S/g
g/010011/s//T/g
g/010100/s//U/g
g/010101/s//V/g
g/010110/s//W/g
g/010111/s//X/g
g/011000/s//Y/g
g/011001/s//Z/g
g/011010/s//a/g
g/011011/s//b/g
g/011100/s//c/g
g/011101/s//d/g
g/011110/s//e/g
g/011111/s//f/g
g/100000/s//g/g
g/100001/s//h/g
g/100010/s//i/g
g/100011/s//j/g
g/100100/s//k/g
g/100101/s//l/g
g/100110/s//m/g
g/100111/s//n/g
g/101000/s//o/g
g/101001/s//p/g
g/101010/s//q/g
g/101011/s//r/g
g/101100/s//s/g
g/101101/s//t/g
g/101110/s//u/g
g/101111/s//v/g
g/110000/s//w/g
g/110001/s//x/g
g/110010/s//y/g
g/110011/s//z/g
g/110100/s//0/g
g/110101/s//1/g
g/110110/s//2/g
g/110111/s//3/g
g/111000/s//4/g
g/111001/s//5/g
g/111010/s//6/g
g/111011/s//7/g
g/111100/s//8/g
g/111101/s//9/g
g/111110/s//+/g
g/111111/s//\//g
g/[[:space:]]/s///g
,p
Q</syntaxhighlight>

{{out}}

<pre>
$ cat base64-encode.ed | ed -E base64-encode.input
Newline appended
108
I am sending you my book,
O my reader, today;
It's up to you to read
Or throw it away.
-- Yeghishe Charents
?
Unknown command
SSBhbSBzZW5kaW5nIHlvdSBteSBib29rLApPIG15IHJlYWRlciwgdG9kYXk7Ckl0J3MgdXAgdG8geW91IHRvIHJlYWQKT3IgdGhyb3cgaXQgYXdheS4KLS0gWWVnaGlzaGUgQ2hhcmVudHMK
?
Warning: buffer modified
</pre>



=={{header|Elixir}}==
=={{header|Elixir}}==