News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

convert integer to base 64 and back again

Started by kevin, October 19, 2023, 09:00:42 AM

Previous topic - Next topic

kevin


 This PlayBASIC source code provides two functions; one to convert a base 10 value into base 64 and the inverse function to convert a base 64 back to base 10

 

PlayBASIC Code: [Select]
   //  characters used to represent each digit in our base 64 number
global Digits_key$="0123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ_@"

// table to help use convert back from numbers in our base 64 string
dim Decode_Digits(255)
for lp=1 to len(Digits_key$)
ThisCHR=mid(Digits_Key$,lp)
Decode_Digits(ThisCHR)=lp-1
next



for lp =0 to 10
Value=rnd($7fffffff)
key$=EncodeInt64(Value)
Result =DecodeInt64(Key$)
print "Value="+str$(Value)+ " "+Key$+" = "+str$(Result)
next



print "Range Test Start."

max=$7fff
For lp =0 to max
key$=EncodeInt64(lp)
Value =DecodeInt64(Key$)
if lp<>Value
print "error: " + key$ + " = " +str$(lp)+" <> "+str$(Value)
endif
if abs(timer()-CurrentTime) >200
CurrentTime=timer()
Sync
endif
next

print "Test Over ......"

sync
waitkey


Function EncodeInt64(ThisValue)
// max digits within a 32bit integer
Digits= 32/6
for Digit=0 to Digits-1
Value= ThisValue and 63
ThisValue = ThisValue >> 6
Result$ =mid$(Digits_key$,Value+1)+Result$
next
EndFunction Result$


Function DecodeInt64(Number$)
for Digit=1 to Len(Number$)
Value= Decode_Digits(mid(Number$,Digit))
Result = (Result << 6) or Value
next
EndFunction Result