November 28, 2020, 10:57:10 AM

### Author Topic: [bb] Base 64 Encoder/Decoder Functions by Andy_A [ 1+ years ago ]  (Read 761 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] Base 64 Encoder/Decoder Functions by Andy_A [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : Base 64 Encoder/Decoder Functions
Author : Andy_A
Posted : 1+ years ago

Description : Mangus wrote the Encoding Function and left this as an exercise to write the Decoding Function.  Here it is.

Code :
Code: BlitzBasic
1. ;########     BASE 64 ENCODER by Mangus     ###########
2. ;########   BASE 64 DECODER by Andy Amaya   ###########
3.
4. org\$ = "I have detected a Fatal Mouse Error - shall I spank the cat?"
5. Print "String to Encode:"
6. Print org\$
7. Print""
8.
9. Print "Encoded string:"
10. Print b64enc\$(org\$) ;little example ;)
11. Print""
12.
13.
14. msg\$ = b64enc\$(org\$)
15. Print "Decoded string:"
16. Print b64dec\$(msg\$)
17. Input()
18.
19.
20. Function b64enc\$(a\$)
21.         b64\$="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
22.         m\$=""
23.         f\$=""
24.         largo=Len(a\$)
25. ;Encode a\$ into one long string of bits
26.         cx\$=""
27.         For encode=1 To largo
28.                 x\$=Mid\$(a\$,encode,1)  ;get one char at a time
29.                 Tx=Asc(x\$)            ;Tx = ASCII code
30.                 b\$=Bin\$(Tx)           ;convert Tx into string of 32 bits
31.                 b\$=Right\$(b\$,8)       ;get the right most 8 bits out of the 32 bits
32.                 cx\$=cx\$+b\$            ;add string of 8 bits to cx\$
33.         Next
34.         ;largo = number of bits stored in cx\$
35.         largo=Len(cx\$)
36.         For encode=1 To largo Step 6
37.                 x\$=Mid\$(cx\$,encode,6)
38.                 bbb=Len(x\$)
39.                 bbbx=6-bbb              ;check for 6 bits
40.                         ;If not full 6 bits at end of bit string, then add "=" to end of encoded string
41.                         If bbbx>0 Then
42.                                 f\$="="
43.                         EndIf
44.                 x\$=x\$ + Left\$("00000000",bbbx)                  ;pad with zeroes to make 6 bits
45.                 res=0
46.                 For y=0 To 5
47.                         by  = Asc(Mid\$(x\$, 6-y, 1)) - 48        ;get bits from right to left (least significant to most)
48.                         res = res + ( 2^y * by)                         ;raise to power of 2 and add to res (result)
49.                 Next
50.                 m\$=m\$+Mid\$(b64\$,res+1,1)+f\$
51.         Next
52.         Return m\$
53. End Function
54.
55. Function b64dec\$(a\$)
56.         b64\$="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
57.         m\$=""
58.         f\$=""
59.         length = Len(a\$)
60.         cx\$ = ""
61.         If Right\$(a\$,1) = "=" Then length = length - 2: flag = 1
62.
63.         For decode = 1 To length
64.                 x\$ = Mid\$(a\$,decode,1)
65.                 pos = Instr(b64\$,x\$)-1
66.                 b\$ = Bin\$(pos)
67.                 b\$ = Right\$(b\$,6)
68.                 cx\$ = cx\$ + b\$
69.         Next
70.
71.         If flag = 1 Then
72.                 numBits = 6 * (Len(a\$)-2)
73.                 rmdr = (Floor(numBits/8)+1)*8-numBits
74.                 oddChar\$ = Mid\$(a\$,Len(a\$)-1,1)
75.                 pos =Instr(b64\$, oddChar\$)-1
76.                 b\$ = Right\$(Bin\$(pos),6)
77.                 b\$ = Left\$(b\$,rmdr)
78.                 cx\$ = cx\$ + b\$
79.         End If
80.
81.         length = Len(cx\$)
82.
83.         For decode = 1 To length Step 8
84.                 b\$ = Mid\$(cx\$,decode,8)
85.                 res = 0
86.                 For y = 0 To 7
87.                         bit =Asc( Mid\$(b\$,8-y,1)) - 48
88.                         If bit <> 0 Then
89.                                 res = res Or (2^y * bit)
90.                         End If
91.                 Next
92.                 m\$ = m\$ + Chr\$(res)
93.         Next
94.         Return m\$
95. End Function

mangus(Posted 1+ years ago)

Great job Andy... I am thinking that you were needing this for the same purpose that me? (making an emailer?)

Andy_A(Posted 1+ years ago)

Actually did this because it seemed to me to be an open challenge by Mangus to complete the code, so I did. Blitz was also at the time very new to me, this provided a good opportunity to investigate Blitz code syntax.The more I think about it, could also be used for a creating a Blitz "PostIt" type program.Glad you can find a good use for it.

Andy_A(Posted 1+ years ago)

It took me a while to notice you're the guy who wrote the encoding function. Thanks!Could I have your permission to post the encoding function on the BlitzCoder website?

mangus(Posted 1+ years ago)

Of course.. consider it yours! make this an open source world oh yeah!

virtlands(Posted 1+ years ago)

<bookmarked>

Andy_A(Posted 1+ years ago)

@VirtlandsI would recommend using the encoding and decoding functions from this post <a href="http://www.blitzmax.com/codearcs/codearcs.php?code=3026" target="_blank">http://www.blitzmax.com/codearcs/codearcs.php?code=3026[/url]as it's much more efficient (faster).