forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve Base16 Codebase (TheAlgorithms#3534)
* Add doctest and remove input() usage * Apply suggestions from code review Co-authored-by: Dhruv Manilawala <[email protected]>
- Loading branch information
1 parent
49d0c41
commit 9bf7b18
Showing
1 changed file
with
14 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,22 @@ | ||
import base64 | ||
|
||
|
||
def main(): | ||
inp = input("->") | ||
def encode_to_b16(inp: str) -> bytes: | ||
""" | ||
Encodes a given utf-8 string into base-16. | ||
>>> encode_to_b16('Hello World!') | ||
b'48656C6C6F20576F726C6421' | ||
>>> encode_to_b16('HELLO WORLD!') | ||
b'48454C4C4F20574F524C4421' | ||
>>> encode_to_b16('') | ||
b'' | ||
""" | ||
encoded = inp.encode("utf-8") # encoded the input (we need a bytes like object) | ||
b16encoded = base64.b16encode(encoded) # b16encoded the encoded string | ||
print(b16encoded) | ||
print(base64.b16decode(b16encoded).decode("utf-8")) # decoded it | ||
return b16encoded | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
import doctest | ||
|
||
doctest.testmod() |