Ehh... I made base64 encoding\decoding in scar..
Have phun! ^^.
SCAR Code:program Base64;
const
str = 'test'; //Fill in the string.
const
Base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
function Base64Encode(str : string) : string;
var
ProcessStr : String;
L,Pads,i : integer;
begin;
l := Length(str);
ProcessStr := str;
if L < 3 then
begin;
Pads := 3 - L;
ProcessStr := ProcessStr + StringOfChar(#0,3 - L mod 3);
L := 3;
end;
if L mod 3 <> 0 then
begin;
Pads := Pads + (3 - L mod 3);
L := L + (3 - L mod 3);
ProcessStr := ProcessStr + StringOfChar(#0,3 - L mod 3);
end;
for i := 1 to L-2 do
begin;
result := result + Base64chars[ord(ProcessStr[i]) and $fc shr 2+1];
result := result + Base64chars[ord(ProcessStr[i]) and 3 shl 4 or ord(ProcessStr[i+1]) and $f0 shr 4+1];
result := result + Base64chars[ord(ProcessStr[i+1]) and $f shl 2 or ord(ProcessStr[i+2]) shr 6+1];
result := result + Base64chars[ord(ProcessStr[i+2]) and $3f+1];
i := i + 2;
end;
l := length(result);
if Pads > 0 then
for i := l downto (l-pads+1) do
Result[i] := '=';
end;
function CharPos( Charz : char) : integer;
begin;
Result := pos(charz,Base64Chars)-1;
end;
function Base64Decode(str : string) : string;
var
L,Pads,i : integer;
begin;
l := Length(str);
if (L<4) or ( l mod 4 <> 0 ) then
Exit;
for i := l downto 1 do
if str[i] = '=' then
Inc(Pads)
else
Break;
for i := 1 to L-3 do
begin;
Result := Result + chr( CharPos(str[i]) shl 2 or charPos(str[i+1]) shr 4);
Result := Result + chr( CharPos(str[i+1]) shl 4 or charPos(str[i+2]) shr 2);
Result := Result + chr( CharPos(str[i+2]) shl 6 or CharPos(str[i+3]) );
i := I + 3;
end;
SetLength(result,Length(result)-pads);
end;
var
Base64 : string;
begin
Base64 := Base64Encode(str);
Writeln('Encoded : ' + Base64);
Writeln('Decoded : ' + Base64decode(base64));
end.








Reply With Quote


















