This week's code snippet, Base64 Encode Decode in Pascal, is brought to you by Subete and the Sample Programs repo.
program Base64EncodeDecode;
{$mode objfpc}{$H+}
uses
base64,
Classes,
SysUtils;
procedure Usage;
begin
Writeln('Usage: please provide a mode and a string to encode/decode');
Halt(1);
end;
function IsBase64Char(c: char): boolean;
begin
Result := (c in ['A'..'Z', 'a'..'z', '0'..'9', '+', '/', '=']);
end;
function IsValidBase64(const s: string): boolean;
var
i, L, padCount, firstPadPos: integer;
begin
L := Length(s);
if (L = 0) or (L mod 4 <> 0) then
Exit(False);
for i := 1 to L do
if not IsBase64Char(s[i]) then
Exit(False);
padCount := 0;
for i := L downto 1 do
if s[i] = '=' then
Inc(padCount)
else
Break;
if padCount > 2 then
Exit(False);
firstPadPos := Pos('=', s);
if (firstPadPos > 0) and (firstPadPos <= L - padCount) then
Exit(False);
Result := True;
end;
var
mode, textarg, outstr: string;
begin
if ParamCount <> 2 then
Usage;
mode := LowerCase(ParamStr(1));
textarg := ParamStr(2);
if textarg = '' then
Usage;
if mode = 'encode' then
begin
outstr := EncodeStringBase64(textarg);
Writeln(outstr);
end
else if (mode = 'decode') then
begin
if not IsValidBase64(textarg) then
Usage;
outstr := DecodeStringBase64(textarg);
if outstr = '' then
Usage;
Writeln(outstr);
end
else
Usage;
end.Below you'll find an up-to-date list of articles by me on The Renegade Coder. For ease of browsing, emojis let you know the article category (i.e., blog: ✒️, code: 💻, meta: 💭, teach: 🍎)
- ✒️ What It Feels Like to Be a Toddler Again: Learning a Language
- ✒️ Things I Don’t Want AI To Help Me With
- ✒️ Why I Rebel Against the Use of Generative AI
- ✒️ Buying a House Sucks
- ✒️ Smug Yet Unserious
- ✒️ 32 College Stories That Always Make Friends Laugh
- 💻 Why Does == Sometimes Work on Integer Objects in Java?
- 🍎 Online Exams Might Be Cooked
- 🍎 Encouraging Attendance With Peer Instruction
- ✒️ Conspiracy Theory: All Pro Sports Are Rigged Now
Also, here are some fun links you can use to support my work.
This document was automatically rendered on 2026-03-13 using SnakeMD.





