Base64 encode data: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added the REXX language. -- ~~~~)
Line 68:
return toBase64(stringToArrayUnicode("Nothing seems hard to the people who don't know what they're talking about."))
}())</lang>
 
=={{header|REXX}}==
<lang rexx>/*REXX program converts text (from a file or CL) to a base64 text string*/
parse arg iFID @ /*get optional arguments. */
if iFID=='' then iFID='favicon.ico' /*use the default input file. */
chunk=10000 /*amount of bytes to read a file.*/
if @='' then /* [↓] read the input file ──►@ */
do s=1 by chunk until y==''; y=charin(iFID,s,chunk); @=@||y; end
t=base64(@)
say center(' input', 79, '─'); say @ /*show header & the input text.*/
say center('base64', 79, '─'); say t /* " " " " base64 " */
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────BASE64 subroutine───────────────────*/
base64: procedure; parse arg x; $= /*get the input string, nullify $*/
z='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
do i=0 for 64; !.i=substr(z,i+1,1); end /*assign base64 array*/
L=length(x)*8 /*save length of b for later.*/
b=x2b(c2x(x))0000000000000000 /*X──►binary, add some 0 padding.*/
 
do j=1 by 6 to L /*traipse through the bit string.*/
_=x2d( b2x( substr(b, j, 6) )) /*compute index into BASE64 table*/
$=$ || !._ /*append this to $ (the output).*/
end /*j*/
 
return $||copies('=', 2*(L//6==2)+(L//6==4)) /*maybe append equal signs*/</lang>
For the various outputs, serveral input texts from the [[http://en.wikipedia.org/wiki/Base64|wiki/Base64]] were used to demonstrate how padding works.
<br><br>
'''output''' when using the input of: <tt> , any carnal pleasure. </tt>
<pre style="overflow:scroll">
──────────────────────────────────── input─────────────────────────────────────
any carnal pleasure.
────────────────────────────────────base64─────────────────────────────────────
YW55IGNhcm5hbCBwbGVhc3VyZS4=
</pre>
'''output''' when using the input of: <tt> , any carnal pleasure </tt>
<pre style="overflow:scroll">
──────────────────────────────────── input─────────────────────────────────────
any carnal pleasure
────────────────────────────────────base64─────────────────────────────────────
YW55IGNhcm5hbCBwbGVhc3VyZQ==
</pre>
'''output''' when using the input of: <tt> , any carnal pleasur </tt>
<pre style="overflow:scroll">
──────────────────────────────────── input─────────────────────────────────────
any carnal pleasur
────────────────────────────────────base64─────────────────────────────────────
YW55IGNhcm5hbCBwbGVhc3Vy
</pre>
'''output''' when using the input of: <tt> , any carnal pleasu </tt>
<pre style="overflow:scroll">
──────────────────────────────────── input─────────────────────────────────────
any carnal pleasu
────────────────────────────────────base64─────────────────────────────────────
YW55IGNhcm5hbCBwbGVhc3U=
</pre>
'''output''' when using the input of: <tt> , any carnal pleas </tt>
<pre style="overflow:scroll">
──────────────────────────────────── input─────────────────────────────────────
any carnal pleas
────────────────────────────────────base64─────────────────────────────────────
YW55IGNhcm5hbCBwbGVhcw==
</pre>
 
=={{header|Tcl}}==