Skip to content

Instantly share code, notes, and snippets.

@dooyeoung
Last active August 3, 2022 12:40
Show Gist options
  • Save dooyeoung/e82ded90d962f6c5152e458aa7c7d837 to your computer and use it in GitHub Desktop.
Save dooyeoung/e82ded90d962f6c5152e458aa7c7d837 to your computer and use it in GitHub Desktop.
base62
# 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
# [0-9,a-z,A-Z] 62자로 구성되며 예측 난이도를 높이기 위해 무작위로 섞습니다.
BASE62 = "dCsuHTIWEcOPLhrgwBpRx4flv8JMKGDVmQ01qtejY9iNFzn5okX63aZAbySU27"
def encode(num, alphabet=BASE62):
if num == 0:
return alphabet[0]
arr = []
base = len(alphabet)
while num:
num, rem = divmod(num, base)
arr.append(alphabet[rem])
arr.reverse()
return ''.join(arr)
def decode(string, alphabet=BASE62):
base = len(alphabet)
strlen = len(string)
num = 0
idx = 0
for char in string:
power = (strlen - (idx + 1))
num += alphabet.index(char) * (base ** power)
idx += 1
return num
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment