Skip to content

Commit

Permalink
Added base64 to binary conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
coolsidd committed Oct 20, 2024
1 parent 03a4251 commit d6b4329
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions conversions/base64_to_binary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
B64_TO_BITS = {
"A": "000000",
"B": "000001",
"C": "000010",
"D": "000011",
"E": "000100",
"F": "000101",
"G": "000110",
"H": "000111",
"I": "001000",
"J": "001001",
"K": "001010",
"L": "001011",
"M": "001100",
"N": "001101",
"O": "001110",
"P": "001111",
"Q": "010000",
"R": "010001",
"S": "010010",
"T": "010011",
"U": "010100",
"V": "010101",
"W": "010110",
"X": "010111",
"Y": "011000",
"Z": "011001",
"a": "011010",
"b": "011011",
"c": "011100",
"d": "011101",
"e": "011110",
"f": "011111",
"g": "100000",
"h": "100001",
"i": "100010",
"j": "100011",
"k": "100100",
"l": "100101",
"m": "100110",
"n": "100111",
"o": "101000",
"p": "101001",
"q": "101010",
"r": "101011",
"s": "101100",
"t": "101101",
"u": "101110",
"v": "101111",
"w": "110000",
"x": "110001",
"y": "110010",
"z": "110011",
"0": "110100",
"1": "110101",
"2": "110110",
"3": "110111",
"4": "111000",
"5": "111001",
"6": "111010",
"7": "111011",
"8": "111100",
"9": "111101",
"+": "111110",
"/": "111111",
}


def base64_to_bin(base64_str: str) -> str:
"""Convert a base64 value to its binary equivalent.
>>> base64_to_bin("ABC")
'000000000001000010'
>>> base64_to_bin(" -abc ")
'-011010011011011100'
>>> base64_to_bin(" a-bc ")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 15, in base64_to_bin
ValueError: Invalid base64 string. Invalid char - at pos 1
>>> base64_to_bin("")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 8, in base64_to_bin
ValueError: Empty string was passed to the function
"""
b64_str = str(base64_str).strip()
if not b64_str:
raise ValueError("Empty string was passed to the function")
is_negative = b64_str[0] == "-"
if is_negative:
b64_str = b64_str[1:]
bin_str = ""
for i, x in enumerate(b64_str):
if not x in B64_TO_BITS.keys():

Check failure on line 95 in conversions/base64_to_binary.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (SIM118)

conversions/base64_to_binary.py:95:16: SIM118 Use `key in dict` instead of `key in dict.keys()`

Check failure on line 95 in conversions/base64_to_binary.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E713)

conversions/base64_to_binary.py:95:16: E713 Test for membership should be `not in`
raise ValueError(f"Invalid base64 string. Invalid char {x} at pos {i}")

Check failure on line 96 in conversions/base64_to_binary.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (EM102)

conversions/base64_to_binary.py:96:30: EM102 Exception must not use an f-string literal, assign to variable first
bin_str += B64_TO_BITS[x]
if is_negative:
bin_str = "-" + bin_str
return bin_str


if __name__ == "__main__":
import doctest

doctest.testmod()

0 comments on commit d6b4329

Please sign in to comment.