Skip to content

Add decode function to base16.py #5575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 26, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions ciphers/base16.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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!')
Expand All @@ -16,6 +17,21 @@ def encode_to_b16(inp: str) -> bytes:
return b16encoded


def decode_from_b16(b16encoded: bytes) -> str:
"""
Decodes from base-16 to a utf-8 string.

>>> decode_from_b16(b'48656C6C6F20576F726C6421')
'Hello World!'
>>> decode_from_b16(b'48454C4C4F20574F524C4421')
'HELLO WORLD!'
>>> decode_from_b16(b'')
''
"""
encoded = base64.b16decode(b16encoded) # b16decoded the input
return encoded.decode("utf-8") # decoded the bytes like object to a string


if __name__ == "__main__":
import doctest

Expand Down