Split base85.py into functions, Add doctests (#5746)

* Update base16.py

* Rename base64_encoding.py to base64.py

* Split into functions, Add doctests

* Update base16.py
This commit is contained in:
Rohan R Bharadwaj
2021-11-02 15:40:25 +05:30
committed by GitHub
parent 24731b078c
commit bdd135d403
3 changed files with 35 additions and 15 deletions

View File

@ -1,30 +1,30 @@
import base64
def encode_to_b16(inp: str) -> bytes:
def base16_encode(inp: str) -> bytes:
"""
Encodes a given utf-8 string into base-16.
>>> encode_to_b16('Hello World!')
>>> base16_encode('Hello World!')
b'48656C6C6F20576F726C6421'
>>> encode_to_b16('HELLO WORLD!')
>>> base16_encode('HELLO WORLD!')
b'48454C4C4F20574F524C4421'
>>> encode_to_b16('')
>>> base16_encode('')
b''
"""
# encode the input into a bytes-like object and then encode b16encode that
return base64.b16encode(inp.encode("utf-8"))
def decode_from_b16(b16encoded: bytes) -> str:
def base16_decode(b16encoded: bytes) -> str:
"""
Decodes from base-16 to a utf-8 string.
>>> decode_from_b16(b'48656C6C6F20576F726C6421')
>>> base16_decode(b'48656C6C6F20576F726C6421')
'Hello World!'
>>> decode_from_b16(b'48454C4C4F20574F524C4421')
>>> base16_decode(b'48454C4C4F20574F524C4421')
'HELLO WORLD!'
>>> decode_from_b16(b'')
>>> base16_decode(b'')
''
"""
# b16decode the input into bytes and decode that into a human readable string