The Game Creators
The Game Creators Home Online Shop Click to Login
  Hot: Christmas CompetitionNovember NewsletterModel Pack 36DB Pro Pack 2009DGS BonanzaCharacter PackFPS Creator Bonanza;
The Game Creators
Code Snippets / base64 encoder and decoder

Go to the first page of this board Return to the Forum Menu Post Message
5 Messages - Page   of 1   
Bookmark and Share Search the Forum

Author Message
Phaelax

User


Joined: Wed Apr 16th 2003
Location: Ohio
Posted: 26th Mar 2006 23:23     Edited: 2nd Apr 2006 00:30     | link | toggle

Encodes and decodes base64 strings.

+ Code Snippet
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


Back to top
Zimnox Find user on ICQ Send AIM user a message
Report this message as abusive
MartinS

User


Joined: Thu Dec 15th 2005
Location: Rochester, NY
Posted: 1st Apr 2006 22:34           | link | toggle

Nice! Just need to find a use for it.

-Martin

Back to top
MartinS\' Music
Report this message as abusive
Google Ad
Back to top
 
Virtual X

User


Joined: Mon Feb 27th 2006
Location: Cyberspace
Posted: 2nd Apr 2006 10:48           | link | toggle

when you send attachments via the internet, you could encode them with Base64 encoding, basically many attachments are not compatible for transport over the SMTP protocol, so Base64 is handy at making sure your file is sent with corruption or atleast thats the theory lol
Back to top
Report this message as abusive
Virtual X

User


Joined: Mon Feb 27th 2006
Location: Cyberspace
Posted: 2nd Apr 2006 10:49           | link | toggle

sorry, WITHOUT corruption lol
Back to top
Report this message as abusive
Virtual X

User


Joined: Mon Feb 27th 2006
Location: Cyberspace
Posted: 2nd Apr 2006 10:53           | link | toggle

the downside though is that it will inflate your attachments to about 33% larger than its original size, but the advantages out way the disadvantages.
Back to top
Report this message as abusive

Go to the first page of this board Return to the Forum Menu Post Message
5 Messages - Page   of 1   
Search the Forum

Sorry, but it has been so long since anyone replied to this Thread that it has been automatically locked.
You may read it but not reply.

Forum Search

Enter a word or phrase to search our Forum for:

Thread Subject Search
Search Phrase:
Search Scope: Entire forum
Just this board
 
Google Forum Search
Search Phrase:
 
Apollo v2.02


Game Creator Store
Privacy Policy AUP Top of Page