Delphi code port

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]

This should do the trick Amando…

[code]Function DecodeBase32(strin as string) As String
dim base32chars as string = “ABCDEFGHIJKLMNOPQRSTUVWXYZ234567”
dim stringa as string
dim i, index, offset as integer
dim words as uint8
dim resultArr() as uint8
dim result as string

if Len(strin) < 32 then

redim resultArr(len(strin))

index = 0
offset = 1
for i = 1 to len(strin)  
  stringa = mid(strin, i, 1)
  words = instr(1, base32chars, Uppercase(stringa))
  if words >= 1 then
    words = words - 1
    if (index <= 3) then 
      index = (index + 5) mod 8
      if (index = 0) then 
        resultArr(offset) =resultArr(offset) or words
        offset = offset + 1
      else 
        resultArr(offset) =resultArr(offset) or ShiftLeft(words, 8 - index)
      end if
    else
      index = (index + 5) mod 8
      resultArr(offset) = resultArr(offset) or ShiftRight(words, index)
      offset = offset + 1
      resultArr(offset) = resultArr(offset) or ShiftLeft(words, 8 - index)
    end if
    
  end if
  
next i 

end if

for i = 1 to resultarr.Ubound
result = result + chr(resultArr(i))
next i

return result
End Function
[/code]

Awesome Alwyn!! Bravo! Works like a charm! I really really appreciate your help. Turbo-Fast coder Alwyn :slight_smile:

You’re welcome :wink:

Yes, thanks Alwyn, just what I was looking for!