Skip to content

Instantly share code, notes, and snippets.

@mgd020
Created March 1, 2018 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgd020/1aeae2b8eadd72db0ddf9daed219f43e to your computer and use it in GitHub Desktop.
Save mgd020/1aeae2b8eadd72db0ddf9daed219f43e to your computer and use it in GitHub Desktop.
# 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