Skip to content

Commit 60aca93

Browse files
tanyonghedhruvmanila
authored andcommitted
Improve Base16 Codebase (TheAlgorithms#3534)
* Add doctest and remove input() usage * Apply suggestions from code review Co-authored-by: Dhruv Manilawala <[email protected]>
1 parent b72d2cf commit 60aca93

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

Diff for: ciphers/base16.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
import base64
22

33

4-
def main():
5-
inp = input("->")
4+
def encode_to_b16(inp: str) -> bytes:
5+
"""
6+
Encodes a given utf-8 string into base-16.
7+
>>> encode_to_b16('Hello World!')
8+
b'48656C6C6F20576F726C6421'
9+
>>> encode_to_b16('HELLO WORLD!')
10+
b'48454C4C4F20574F524C4421'
11+
>>> encode_to_b16('')
12+
b''
13+
"""
614
encoded = inp.encode("utf-8") # encoded the input (we need a bytes like object)
715
b16encoded = base64.b16encode(encoded) # b16encoded the encoded string
8-
print(b16encoded)
9-
print(base64.b16decode(b16encoded).decode("utf-8")) # decoded it
16+
return b16encoded
1017

1118

1219
if __name__ == "__main__":
13-
main()
20+
import doctest
21+
22+
doctest.testmod()

0 commit comments

Comments
 (0)