Created
October 14, 2019 11:45
-
-
Save squizduos/a9b6a8a00246fc6ac576658303656efc to your computer and use it in GitHub Desktop.
Encode integer to short url-friendly string with Python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import string | |
def hex_encode_int(n): | |
alphabet = string.digits + string.ascii_uppercase + string.ascii_lowercase | |
result = "" | |
while n: | |
n, t = divmod(n, len(alphabet)) | |
result += alphabet[t] | |
return result or "0" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment