Tuesday, April 23, 2024
HomeSoftware EngineeringEasy methods to do Base64 Encoding in Python

Easy methods to do Base64 Encoding in Python

The problem

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

Don’t use built-in features.


# ought to return 'dGhpcyBpcyBhIHN0cmluZyEh'
to_base_64('it is a string!!')

# ought to return 'it is a string!!'

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]).change("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]).change("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)]) )

Check circumstances to validate our answer

from answer import to_base_64, from_base_64
import take a look at

@take a look at.describe("Fundamental exams")
def _():
    @take a look at.it("Checks")
    def __():
        circumstances = [["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"],
          ["the quick brown fox jumps over the white fence. ","dGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSB3aGl0ZSBmZW5jZS4g"],

        for x, y in circumstances:
            take a look at.assert_equals(consequence, y)
            take a look at.assert_equals(from_base_64(consequence), x)


Please enter your comment!
Please enter your name here

Most Popular

Recent Comments