-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathbase62.py
More file actions
52 lines (41 loc) · 1.17 KB
/
base62.py
File metadata and controls
52 lines (41 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import string
def base62_encode(num: int) -> str:
"""
Encodes a positive integer into a base62 string.
"""
if num == 0:
return "0"
arr = []
base = 62
charset = string.digits + string.ascii_lowercase + string.ascii_uppercase
while num:
num, rem = divmod(num, base)
arr.append(charset[rem])
arr.reverse()
return "".join(arr)
def base62_decode(string_val: str) -> int:
"""
Decodes a base62 string into a positive integer.
"""
base = 62
charset = string.digits + string.ascii_lowercase + string.ascii_uppercase
strlen = len(string_val)
num = 0
for idx, char in enumerate(string_val):
power = strlen - (idx + 1)
num += charset.index(char) * (base**power)
return num
def test_base62() -> None:
"""
Tests for base62_encode and base62_decode.
"""
assert base62_encode(0) == "0"
assert base62_encode(123) == "1z"
assert base62_encode(1000000) == "4C92"
assert base62_decode("0") == 0
assert base62_decode("1z") == 123
assert base62_decode("4C92") == 1000000
if __name__ == "__main__":
import doctest
doctest.testmod()
test_base62()