Created
March 1, 2018 11:32
-
-
Save mgd020/1aeae2b8eadd72db0ddf9daed219f43e to your computer and use it in GitHub Desktop.
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
# Turn an integer into a nice base64 encoded string | |
# E.g. 4095 -> D_8 | |
# requires python 3 | |
import base64 | |
def encode(n): | |
return base64.urlsafe_b64encode(n.to_bytes((n.bit_length() + 7) // 8, 'big')).rstrip(b'==') | |
def decode(n): | |
return int.from_bytes(base64.urlsafe_b64decode(n + ('=' * ((4 - len(n)) % 4))), 'big') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment