## The problem

Create a operate that converts the worth of the String to and from Base64 utilizing the ASCII character set.

Don’t use built-in capabilities.

**Examples:**

```
# ought to return 'dGhpcyBpcyBhIHN0cmluZyEh'
to_base_64('this can be a string!!')
# ought to return 'this can be a string!!'
from_base_64('dGhpcyBpcyBhIHN0cmluZyEh')
```

You’ll be able to study extra about Base64 encoding and decoding right here.

## The answer in Python code

Choice 1:

```
CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
def to_base_64(string):
padding = 3 - len(string) % 3 if len(string) % 3 else 0
binary = ''.be part of(format(ord(i),'08b') for i in string) + '00'*padding
return ''.be part of(CODES[int(binary[i:i+6], 2)] for i in vary(0, len(binary), 6))
def from_base_64(string):
binary = ''.be part of(format(CODES.discover(i),'06b') for i in string)
return ''.be part of(chr(int(binary[i:i+8], 2)) for i in vary(0, len(binary), 8)).rstrip('x00')
```

Choice 2:

```
import string
base = string.ascii_uppercase+string.ascii_lowercase+string.digits+"+/"
def to_base_64(strng):
ls = "".be part of([bin(ord(i))[2:].zfill(8) for i in strng])
ls2 = [int(ls[i:i+6].ljust(6, '0'),2) for i in vary(0,len(ls),6)]
return "".be part of([base[i%64] for i in ls2]).substitute("x00","")
def from_base_64(strng):
ls = "".be part of([bin(base.index(i))[2:].zfill(6) for i in strng])
ls2 = [int(ls[i:i+8],2) for i in vary(0,len(ls),8)]
return "".be part of([chr(i) for i in ls2]).substitute("x00","")
```

Choice 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)]
desk = caps + lows + nums + ['+', '-']
num24 = lambda n3: sum( [(n3[i] << (16 - (8 * i)) ) for i in vary(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: ''.be part of( [table[c] for c in code(t(s3)) ] )
num6 = lambda n4: sum( [ (n4[i] << (18 - (6 * i) )) for i in vary(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: ''.be part of( chr(c) for c in decode(d(s4)) )
def to_base_64(string):
return ''.be part of( map(to64, [string[i:i+3] for i in vary(0, len(string), 3)]) )
def from_base_64(string):
return ''.be part of( map(from64, [string[i:i+4] for i in vary(0, len(string), 4)]) )
```

## Take a look at instances to validate our resolution

```
from resolution import to_base_64, from_base_64
import check
@check.describe("Fundamental assessments")
def _():
@check.it("Checks")
def __():
instances = [["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 instances:
outcome=to_base_64(x)
check.assert_equals(outcome, y)
check.assert_equals(from_base_64(outcome), x)
```