MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / encodeBase64

Function encodeBase64

ciphers/base64_cipher.py:1–24  ·  view source on GitHub ↗
(text)

Source from the content-addressed store, hash-verified

1def encodeBase64(text):
2 base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
3
4 r = "" #the result
5 c = 3 - len(text) % 3 #the length of padding
6 p = "=" * c #the padding
7 s = text + "\0" * c #the text to encode
8
9 i = 0
10 while i < len(s):
11 if i > 0 and ((i / 3 * 4) % 76) == 0:
12 r = r + "\r\n"
13
14 n = (ord(s[i]) << 16) + (ord(s[i+1]) << 8 ) + ord(s[i+2])
15
16 n1 = (n >> 18) & 63
17 n2 = (n >> 12) & 63
18 n3 = (n >> 6) & 63
19 n4 = n & 63
20
21 r += base64chars[n1] + base64chars[n2] + base64chars[n3] + base64chars[n4]
22 i += 3
23
24 return r[0: len(r)-len(p)] + p
25
26def decodeBase64(text):
27 base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected