The challenge
Create a function that converts the value of the String to and from Base64 using the ASCII character set.
Do not use built-in functions.
Examples:
# should return 'dGhpcyBpcyBhIHN0cmluZyEh'
to_base_64('this is a string!!')
# should return 'this is a string!!'
from_base_64('dGhpcyBpcyBhIHN0cmluZyEh')
You can learn more about Base64 encoding and decoding here.
The solution in Python code
Option 1:
CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
def to_base_64(string):
padding = 3 - len(string) % 3 if len(string) % 3 else 0
binary = ''.join(format(ord(i),'08b') for i in string) + '00'*padding
return ''.join(CODES[int(binary[i:i+6], 2)] for i in range(0, len(binary), 6))
def from_base_64(string):
binary = ''.join(format(CODES.find(i),'06b') for i in string)
return ''.join(chr(int(binary[i:i+8], 2)) for i in range(0, len(binary), 8)).rstrip('\x00')
Option 2:
import string
base = string.ascii_uppercase+string.ascii_lowercase+string.digits+"+/"
def to_base_64(strng):
ls = "".join([bin(ord(i))[2:].zfill(8) for i in strng])
ls2 = [int(ls[i:i+6].ljust(6, '0'),2) for i in range(0,len(ls),6)]
return "".join([base[i%64] for i in ls2]).replace("\x00","")
def from_base_64(strng):
ls = "".join([bin(base.index(i))[2:].zfill(6) for i in strng])
ls2 = [int(ls[i:i+8],2) for i in range(0,len(ls),8)]
return "".join([chr(i) for i in ls2]).replace("\x00","")
Option 3:
caps = [chr(c + 65) for c in range(26)]
lows = [chr(c + 97) for c in range(26)]
nums = [str(n) for n in range(10)]
table = caps + lows + nums + ['+', '-']
num24 = lambda n3: sum( [(n3[i] << (16 - (8 * i)) ) for i in range(len(n3))] )
code = lambda n3: [ (num24(n3) >> (18 - (6 * i))) % 64 for i in range(len(n3) + 1) ]
t = lambda s3: [ord(c) for c in s3]
to64 = lambda s3: ''.join( [table[c] for c in code(t(s3)) ] )
num6 = lambda n4: sum( [ (n4[i] << (18 - (6 * i) )) for i in range(len(n4)) ] )
decode = lambda n4: [ (num6(n4) >> (16 - (8 * i) )) % 256 for i in range(len(n4) - 1) ]
d = lambda s4: [table.index(c) for c in s4]
from64 = lambda s4: ''.join( chr(c) for c in decode(d(s4)) )
def to_base_64(string):
return ''.join( map(to64, [string[i:i+3] for i in range(0, len(string), 3)]) )
def from_base_64(string):
return ''.join( map(from64, [string[i:i+4] for i in range(0, len(string), 4)]) )
Test cases to validate our solution
from solution import to_base_64, from_base_64
import test
@test.describe("Basic tests")
def _():
@test.it("Tests")
def __():
cases = [["this is a string!!","dGhpcyBpcyBhIHN0cmluZyEh"],
["this is a test!","dGhpcyBpcyBhIHRlc3Qh"],
["now is the time for all good men to come to the aid of their country.","bm93IGlzIHRoZSB0aW1lIGZvciBhbGwgZ29vZCBtZW4gdG8gY29tZSB0byB0aGUgYWlkIG9mIHRoZWlyIGNvdW50cnku"],
["1234567890 ", "MTIzNDU2Nzg5MCAg"],
["ABCDEFGHIJKLMNOPQRSTUVWXYZ ", "QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVog"],
["the quick brown fox jumps over the white fence. ","dGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSB3aGl0ZSBmZW5jZS4g"],
["dGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSB3aGl0ZSBmZW5jZS4","ZEdobElIRjFhV05ySUdKeWIzZHVJR1p2ZUNCcWRXMXdjeUJ2ZG1WeUlIUm9aU0IzYUdsMFpTQm1aVzVqWlM0"],
["VFZSSmVrNUVWVEpPZW1jMVRVTkJaeUFna","VkZaU1NtVnJOVVZXVkVwUFpXMWpNVlJWVGtKYWVVRm5h"],
["TVRJek5EVTJOemc1TUNBZyAg","VFZSSmVrNUVWVEpPZW1jMVRVTkJaeUFn"]]
for x, y in cases:
result=to_base_64(x)
test.assert_equals(result, y)
test.assert_equals(from_base_64(result), x)