Hi!
Now I got a few minutes to improve the code, now it is down to 35 ms from 180 ms using the original code.
Function Base85Encode(text as String) As String
#pragma BackgroundTasks false
dim padding as integer
dim output() as String
dim padStr As String = chr(0) + chr(0) + chr(0)
for i as integer = 1 to text.lenB step 4
//Split in parts of 4
dim part as String = text.MidB(i,4)
if part.lenB<4 then
padding = 4 - part.lenB
part = leftB(part + padStr, 4)
end
//Convert into a number
dim num as integer
for j as integer = 1 to part.LenB
dim val as integer = ascB(part.midB(j,1))
dim shift as integer = (4-j)*8
num = num + Bitwise.ShiftLeft(val,shift)
next
//Split in potences of 85 and convert to char again
dim subtract as integer
for j as integer = 4 downto 0
dim val as integer = (num-subtract) / ( 85 ^ j)
output.Append chr(val+33)
subtract = subtract + (val * 85 ^j)
next
next
dim s As String = join(output, "")
return "<~"+s.leftB(s.lenB-padding)+"~>"
End Function
Function Base85Decode(text as String) As String
#pragma BackgroundTasks false
if text.leftB(2) = "<~" Then text = text.rightB(text.lenB-2)
if text.rightB(2) = "~>" Then text = text.leftB(text.lenB-2)
text = text.ReplaceAllB(chr(10),"")
text = text.ReplaceAllB(chr(13),"")
dim padding as integer
dim output() as String
for i as integer = 1 to text.lenB step 5
dim part as String = text.MidB(i,5)
if part.lenB < 5 then
padding= 5 - part.lenB
part = LeftB(part + "uuuu", 5)
end
//Convert into a number
dim num as integer
for j as integer = 1 to part.LenB
dim val as Integer = ascB(part.midB(j,1))-33
num = num + val * 85 ^ (5 - j)
next
//Convert number into ascii chars
for j as integer = 24 downto 0 step 8
dim val as integer = Bitwise.ShiftRight(num,j)
//Mask the bits we already used
val = Bitwise.BitAnd(val,&hFF)
output.Append chr(val)
next
next
dim s As String = join(output, "")
return s.leftB(s.lenB-padding)
End Function
Posting was changed due to a bug in the previous code.
HTH,
GreatOm