Skip to content

Commit

Permalink
Fixed an issue where it fails to encode zero (0)
Browse files Browse the repository at this point in the history
  • Loading branch information
suminb committed Sep 15, 2014
1 parent 5567873 commit 5d9f5b3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
9 changes: 6 additions & 3 deletions base62.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@

__author__ = 'Sumin Byeon'
__email__ = '[email protected]'
__version__ = '0.1.2'
__version__ = '0.1.3'

CHARSET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
BASE = 62

def encode(n):
"""Encodes a given integer ``n``."""

s = []
while n > 0:
r = n % BASE
n //= BASE

s.append(CHARSET[r])

s.reverse()
if len(s) > 0:
s.reverse()
else:
s.append('0')

return ''.join(s)

Expand Down
3 changes: 3 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import base62

def test_basic():
assert base62.encode(0) == '0'
assert base62.decode('0') == 0

assert base62.encode(34441886726) == 'base62'
assert base62.decode('base62') == 34441886726

0 comments on commit 5d9f5b3

Please sign in to comment.