Base64 encode data: Difference between revisions

Content added Content deleted
m (BaCon moved to the BASIC section.)
(Added XPL0 example.)
Line 2,274:
....
AAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAE=
</pre>
 
=={{header|XPL0}}==
The end of the input file is detected here by detecting the error when
attempting to read beyond the end. The first attempt to read beyond the
end returns an EOF code ($1A -- useful when reading text files). The
second attempt to read beyond the end causes an error, which by default
aborts a program. However, error trapping is turned off here [with
Trap(false)] and GetErr is used to detect the error, and thus the
end-of-file.
 
The output here is different than other examples because the icon at the
provided link has changed.
<syntaxhighlight lang "XPL0">int Char;
func GetCh; \Return character from input file (with one-char look-ahead)
int Prev;
[Prev:= Char;
Char:= ChIn(3);
return Prev;
];
 
char Base64;
int FD, Acc, Bytes, Column;
[Base64:= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ ";
Trap(false); \disable error trapping; use GetErr instead
 
FD:= FOpen("favicon.ico", 0); \open input file
FSet(FD, ^I);
OpenI(3);
 
FD:= FOpen("favicon.txt", 1); \open file for output
FSet(FD, ^O);
OpenO(3);
 
Char:= ChIn(3); \initialize one-char look-ahead
Column:= 0;
loop [Acc:= 0; Bytes:= 0;
Acc:= GetCh<<16;
if GetErr then quit;
Bytes:= Bytes+1;
Acc:= Acc + GetCh<<8;
if GetErr = 0 then
[Bytes:= Bytes+1;
Acc:= Acc + GetCh;
if GetErr = 0 then Bytes:= Bytes+1;
];
ChOut(3, Base64(Acc>>18));
ChOut(3, Base64(Acc>>12 & $3F));
ChOut(3, if Bytes < 2 then ^= else Base64(Acc>>6 & $3F));
ChOut(3, if Bytes < 3 then ^= else Base64(Acc & $3F));
Column:= Column+4;
if Column >= 76 then [Column:= 0; CrLf(3)];
if Bytes < 3 then quit;
];
if Column # 0 then CrLf(3);
Close(3);
]</syntaxhighlight>
{{out}}
<pre>
AAABAAMAMDAAAAEAIACoJQAANgAAACAgAAABACAAqBAAAN4lAAAQEAAAAQAgAGgEAACGNgAAKAAA
...
AAD8PwAA/D8AAIQhAACH4QAAh+EAAIQhAAD8PwAA/D8AAPw/AAA=
</pre>