Skip to content

Instantly share code, notes, and snippets.

@captainbrosset
Created December 18, 2011 12:04
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 captainbrosset/1493180 to your computer and use it in GitHub Desktop.
Save captainbrosset/1493180 to your computer and use it in GitHub Desktop.
Padless base64 converter
"""The converter module is used to convert a number into a padless base64 string and back.
Padless base64 are used for short urls: http://www.mydomain.com/MTQy for instance.
Base64 strings are normally organized by groups of 4 characters and right padded if necessary with =
Padless base64 means that = characters are removed"""
import base64
def encodeB64Padless(number):
"""Encode an integer into a padless base64 string"""
return base64.b64encode(str(number)).replace("=", "")
def decodeB64Padless(str):
"""Decode a padless base64 string into an integer"""
nbOfBlocks = len(str)/4
for i in range(4 - len(str[nbOfBlocks*4:])): str += "="
try:
return int(base64.b64decode(str))
except:
return None # This means that str was not a valid padless b64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment