File tree 1 file changed +18
-3
lines changed
1 file changed +18
-3
lines changed Original file line number Diff line number Diff line change 4
4
def encode_to_b16 (inp : str ) -> bytes :
5
5
"""
6
6
Encodes a given utf-8 string into base-16.
7
+
7
8
>>> encode_to_b16('Hello World!')
8
9
b'48656C6C6F20576F726C6421'
9
10
>>> encode_to_b16('HELLO WORLD!')
10
11
b'48454C4C4F20574F524C4421'
11
12
>>> encode_to_b16('')
12
13
b''
13
14
"""
14
- encoded = inp .encode ("utf-8" ) # encoded the input (we need a bytes like object)
15
- b16encoded = base64 .b16encode (encoded ) # b16encoded the encoded string
16
- return b16encoded
15
+ # encode the input into a bytes-like object and then encode b16encode that
16
+ return base64 .b16encode (inp .encode ("utf-8" ))
17
+
18
+
19
+ def decode_from_b16 (b16encoded : bytes ) -> str :
20
+ """
21
+ Decodes from base-16 to a utf-8 string.
22
+
23
+ >>> decode_from_b16(b'48656C6C6F20576F726C6421')
24
+ 'Hello World!'
25
+ >>> decode_from_b16(b'48454C4C4F20574F524C4421')
26
+ 'HELLO WORLD!'
27
+ >>> decode_from_b16(b'')
28
+ ''
29
+ """
30
+ # b16decode the input into bytes and decode that into a human readable string
31
+ return base64 .b16decode (b16encoded ).decode ("utf-8" )
17
32
18
33
19
34
if __name__ == "__main__" :
You can’t perform that action at this time.
0 commit comments