Skip to content

Instantly share code, notes, and snippets.

@mgd020 mgd020/pretty_id.py
Created Mar 1, 2018

Embed
What would you like to do?
# 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
You can’t perform that action at this time.