MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / base16_encode

Function base16_encode

ciphers/base16.py:1–15  ·  view source on GitHub ↗

Encodes the given bytes into base16. >>> base16_encode(b'Hello World!') '48656C6C6F20576F726C6421' >>> base16_encode(b'HELLO WORLD!') '48454C4C4F20574F524C4421' >>> base16_encode(b'') ''

(data: bytes)

Source from the content-addressed store, hash-verified

1def base16_encode(data: bytes) -> str:
2 """
3 Encodes the given bytes into base16.
4
5 >>> base16_encode(b'Hello World!')
6 '48656C6C6F20576F726C6421'
7 >>> base16_encode(b'HELLO WORLD!')
8 '48454C4C4F20574F524C4421'
9 >>> base16_encode(b'')
10 ''
11 """
12 # Turn the data into a list of integers (where each integer is a byte),
13 # Then turn each byte into its hexadecimal representation, make sure
14 # it is uppercase, and then join everything together and return it.
15 return "".join([hex(byte)[2:].zfill(2).upper() for byte in list(data)])
16
17
18def base16_decode(data: str) -> bytes:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected