Created
July 2, 2014 05:10
-
-
Save mengzhuo/a97d9828271a35e50cc2 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
#!/usr/bin/env python | |
# encoding: utf-8 | |
""" | |
Base57 Module For Encode and Decode | |
{A..Z} {a..z} {1..9} without | |
'O o 0 1 l I'which chars easily mistype | |
""" | |
# WARNING DON'T CHANGE the BASE | |
BASE = '23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz' | |
BASE_LENGTH = len(BASE) | |
def decode(key=""): | |
"""@todo: Docstring for encode. | |
:key: @todo | |
:returns: @todo | |
""" | |
data = 0 | |
for index, char in enumerate(reversed(key)): | |
data += BASE.index(char) * BASE_LENGTH ** index | |
return data | |
def encode(integer): | |
"""@todo: Docstring for decode. | |
:integer: @todo | |
:returns: @todo | |
""" | |
if integer == 0: | |
return BASE[integer] | |
data = '' | |
while integer > 0: | |
remainder = integer % BASE_LENGTH | |
data += BASE[remainder] | |
integer /= BASE_LENGTH | |
return data[::-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment