Encodes and decodes base64 strings.
REM *************************************************
REM * Original Author: Randy Charles Morin
REM * Author's Website: http://www.kbcafe.com
REM *
REM * Ported from C++ to DBP
REM *
REM * Converted By: Phaelax
REM * Website: http://cca-software.com
REM *************************************************
test$ = "This is a TEST!"
print test$
encoded$ = encode64$(test$)
print encoded$
print decode64$(encoded$)
suspend for key
rem Encodes a string to base64
rem returns an encoded string
function encode64$(encode as string)
b1 as byte
b2 as byte
b3 as byte
value$ = ""
if len(encode) = 0 then exitfunction value$
for i=1 to len(encode) step 3
b1 = 0
b2 = 0
b3 = 0
b1 = asc(mid$(encode,i))
if (i+1 <= len(encode)) then b2 = asc(mid$(encode,i+1))
if (i+2 <= len(encode)) then b3 = asc(mid$(encode,i+2))
b4 as byte
b5 as byte
b6 as byte
b7 as byte
b4 = b1>>2
b5 = ((b1&&0x3)<<4) || (b2>>4)
b6 = ((b2&&0xf)<<2) || (b3>>6)
b7 = b3&&0x3f
value$ = value$ + encodeByte$(b4)
value$ = value$ + encodeByte$(b5)
if i+1 <= len(encode)
value$ = value$ + encodeByte$(b6)
else
value$ = value$ + "="
endif
if i+2 <= len(encode)
value$ = value$ + encodeByte$(b7)
else
value$ = value$ + "="
endif
if i mod 76 = 0
value$ = value$ + chr$(13)+chr$(10)
endif
next i
endfunction value$
rem Decodes a string to base64
rem returns a decoded string
function decode64$(decode as string)
b1 as byte
b2 as byte
b3 as byte
value$ = ""
temp$ = ""
for j = 1 to len(decode)
if isBase64(mid$(decode,j)) then temp$ = temp$ + mid$(decode,j)
next j
if len(temp$) = 0 then exitfunction value$
for i = 1 to len(temp$) step 4
c1$ = "A"
c2$ = "A"
c3$ = "A"
c4$ = "A"
c1$ = mid$(temp$, i)
if i+1 <= len(temp$) then c2$ = mid$(temp$,i+1)
if i+2 <= len(temp$) then c3$ = mid$(temp$,i+2)
if i+3 <= len(temp$) then c4$ = mid$(temp$,i+3)
b1 as byte
b2 as byte
b3 as byte
b4 as byte
b1 = decodeChar(c1$)
b2 = decodeChar(c2$)
b3 = decodeChar(c3$)
b4 = decodeChar(c4$)
value$ = value$ + chr$(b1<<2 || b2>>4)
if c3$ <> "=" then value$ = value$ + chr$(((b2&&0xf)<<4) || (b3>>2))
if c4$ <> "=" then value$ = value$ + chr$(((b3&&0x3)<<6) || b4)
next i
endfunction value$
rem private helper function
rem technically, its a 6-bit pattern
rem with the two higher bits as 0
rem byte as input
rem returns a character
function encodeByte$(b as byte)
if b < 26 then exitfunction chr$(asc("A")+b)
if b < 52 then exitfunction chr$(asc("a")+(b-26))
if b < 62 then exitfunction chr$(asc("0")+(b-52))
if b = 62 then exitfunction "+"
endfunction "/"
rem private helper function
rem character as input
rem returns byte value of decoded char
function decodeChar(c as string)
if c >= "A" and c <= "Z"
v = asc(c)-asc("A")
exitfunction v
endif
if c >= "a" and c <= "z"
v = asc(c)-asc("a")+26
exitfunction v
endif
if c >= "0" and c <= "9"
v = asc(c)-asc("0")+52
exitfunction v
endif
if c = "+" then exitfunction 62
endfunction 63
rem private helper function
rem checks if the character is a
rem valid base64 character
rem returns true or false
function isBase64(c as string)
if c >= "A" and c <= "Z" then exitfunction 1
if c >= "a" and c <= "z" then exitfunction 1
if c >= "0" and c <= "9" then exitfunction 1
if c = "+" then exitfunction 1
if c = "/" then exitfunction 1
if c = "=" then exitfunction 1
endfunction 0
You can use this website to check values if you want.
Javazoom's base64 converter