MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / ascii85_encode

Function ascii85_encode

ciphers/base85.py:16–30  ·  view source on GitHub ↗

>>> ascii85_encode(b"") b'' >>> ascii85_encode(b"12345") b'0etOA2#' >>> ascii85_encode(b"base 85") b'@UX=h+?24'

(data: bytes)

Source from the content-addressed store, hash-verified

14
15
16def ascii85_encode(data: bytes) -> bytes:
17 """
18 >>> ascii85_encode(b"")
19 b''
20 >>> ascii85_encode(b"12345")
21 b'0etOA2#'
22 >>> ascii85_encode(b"base 85")
23 b'@UX=h+?24'
24 """
25 binary_data = "".join(bin(ord(d))[2:].zfill(8) for d in data.decode("utf-8"))
26 null_values = (32 * ((len(binary_data) // 32) + 1) - len(binary_data)) // 8
27 binary_data = binary_data.ljust(32 * ((len(binary_data) // 32) + 1), "0")
28 b85_chunks = [int(_s, 2) for _s in map("".join, zip(*[iter(binary_data)] * 32))]
29 result = "".join(_base10_to_85(chunk)[::-1] for chunk in b85_chunks)
30 return bytes(result[:-null_values] if null_values % 4 != 0 else result, "utf-8")
31
32
33def ascii85_decode(data: bytes) -> bytes:

Callers

nothing calls this directly

Calls 2

_base10_to_85Function · 0.85
decodeMethod · 0.45

Tested by

no test coverage detected