Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ad76b50

Browse files
committedOct 20, 2024·
Added binary to base64 conversion
1 parent 03a4251 commit ad76b50

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
 

‎conversions/binary_to_base64.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
"""
2+
* Author: Siddharth Singh (https://github.com/coolsidd)
3+
* Description: Convert a binary string to Base64.
4+
5+
References for better understanding:
6+
https://en.wikipedia.org/wiki/Base64
7+
"""
8+
9+
BITS_TO_B64 = {
10+
"000000": "A",
11+
"000001": "B",
12+
"000010": "C",
13+
"000011": "D",
14+
"000100": "E",
15+
"000101": "F",
16+
"000110": "G",
17+
"000111": "H",
18+
"001000": "I",
19+
"001001": "J",
20+
"001010": "K",
21+
"001011": "L",
22+
"001100": "M",
23+
"001101": "N",
24+
"001110": "O",
25+
"001111": "P",
26+
"010000": "Q",
27+
"010001": "R",
28+
"010010": "S",
29+
"010011": "T",
30+
"010100": "U",
31+
"010101": "V",
32+
"010110": "W",
33+
"010111": "X",
34+
"011000": "Y",
35+
"011001": "Z",
36+
"011010": "a",
37+
"011011": "b",
38+
"011100": "c",
39+
"011101": "d",
40+
"011110": "e",
41+
"011111": "f",
42+
"100000": "g",
43+
"100001": "h",
44+
"100010": "i",
45+
"100011": "j",
46+
"100100": "k",
47+
"100101": "l",
48+
"100110": "m",
49+
"100111": "n",
50+
"101000": "o",
51+
"101001": "p",
52+
"101010": "q",
53+
"101011": "r",
54+
"101100": "s",
55+
"101101": "t",
56+
"101110": "u",
57+
"101111": "v",
58+
"110000": "w",
59+
"110001": "x",
60+
"110010": "y",
61+
"110011": "z",
62+
"110100": "0",
63+
"110101": "1",
64+
"110110": "2",
65+
"110111": "3",
66+
"111000": "4",
67+
"111001": "5",
68+
"111010": "6",
69+
"111011": "7",
70+
"111100": "8",
71+
"111101": "9",
72+
"111110": "+",
73+
"111111": "/",
74+
}
75+
76+
77+
def bin_to_base64(bin_str: str) -> str:
78+
"""Convert a binary value to its base64 equivalent
79+
80+
81+
>>> bin_to_base64("000001")
82+
'AB'
83+
>>> bin_to_base64("0")
84+
'A'
85+
>>> bin_to_base64("1")
86+
'B'
87+
>>> bin_to_base64("-1")
88+
'-B'
89+
>>> bin_to_base64(" -000001 ")
90+
'-AB'
91+
>>> bin_to_base64("0-0")
92+
Traceback (most recent call last):
93+
...
94+
ValueError: Non-binary value was passed to the function
95+
>>> bin_to_base64("")
96+
Traceback (most recent call last):
97+
...
98+
ValueError: Empty string was passed to the function
99+
"""
100+
bin_str = str(bin_str).strip()
101+
if not bin_str:
102+
raise ValueError("Empty string was passed to the function")
103+
is_negative = bin_str[0] == "-"
104+
if is_negative:
105+
bin_str = bin_str[1:]
106+
if not all(char in "01" for char in bin_str):
107+
raise ValueError("Non-binary value was passed to the function")
108+
bin_str = "0" * (6 * (divmod(len(bin_str), 6)[0] + 1) - len(bin_str)) + bin_str
109+
base64_str = ""
110+
while len(bin_str) > 0:
111+
base64_str += BITS_TO_B64[bin_str[:6]]
112+
bin_str = bin_str[6:]
113+
114+
return "-" + base64_str if is_negative else base64_str
115+
116+
117+
if __name__ == "__main__":
118+
from doctest import testmod
119+
120+
testmod()

0 commit comments

Comments
 (0)
Please sign in to comment.