2018-11-03 20:08:13 +00:00
|
|
|
import base64
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2020-11-23 07:31:43 +00:00
|
|
|
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''
|
|
|
|
"""
|
2019-10-05 05:14:13 +00:00
|
|
|
encoded = inp.encode("utf-8") # encoded the input (we need a bytes like object)
|
|
|
|
b16encoded = base64.b16encode(encoded) # b16encoded the encoded string
|
2020-11-23 07:31:43 +00:00
|
|
|
return b16encoded
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2018-11-03 20:08:13 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
2020-11-23 07:31:43 +00:00
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|