Hi,
I have a snippet of code that is written in Delphi for a DecodeBase32. The EncodeBase32 was ported brilliantly by Tim Hare in this RS forum post .
I am tried converting it to Xojo, but I am bad both in Delphi and Bit maths. I will appreciate your help!
function DecodeBase32(strin:string):string;
var
base32Chars,stringa:string;
i,index,offset:integer;
words:byte;
begin
if length(strin)<32 then exit;
base32Chars:='ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
for i:=1 to 20 do result:=result+CHRNULL;
index:=0;
offset:=1;
for i:=1 to length(strin) do begin
stringa:=strin[i];
words:=pos(uppercase(stringa),base32Chars);
if words<1 then begin
continue;
end;
dec(words);
if (index <= 3) then begin
index:= (index + 5) mod 8;
if (index = 0) then begin
result[offset]:=chr(byte(ord(result[offset]) or words));
inc(offset);
end else result[offset]:=chr(ord(result[offset]) or byte(words shl (8 - index)));
end else begin
index:= (index + 5) mod 8;
result[offset]:=chr(ord(result[offset]) or byte(words shr index));
inc(offset);
result[offset]:=chr(ord(result[offset]) or byte(words shl (8 - index)));
end;
end;
end;
The EncodeBase32 ported to RS by Tim Hare (runs perfectly in Xojo) is here for anyone who needs it.
[code]Function EncodeBase32(strin as string) As string
dim i, index as integer
dim words as integer
dim base32chars as string = “ABCDEFGHIJKLMNOPQRSTUVWXYZ234567”
dim result as string
index= 0
i= 1
while i<= len(strin)
if index> 3 then
words= bitwiseAnd(asc(mid(strin, i, 1)), bitwise.ShiftRight(&hFF, index))
index= (index+ 5) mod 8
words= bitwise.ShiftLeft(words, index)
if i< len(strin) then
words= bitwise.BitOr(words, bitwise.ShiftRight(asc(mid(strin, i+1, 1)), 8-index))
end
i= i+ 1
else
words= bitwise.BitAnd(bitwise.ShiftRight(asc(mid(strin, i)), 8-(index+5)), &h1F)
index= (index+ 5) mod 8
if index= 0 then i= i+ 1
end
result= result+ mid(base32chars, words+1, 1)
wend
return result
End Function
[/code]