Skip to content

Commit 75f9eb3

Browse files
committed
Fixed minor bug
1 parent b90f53d commit 75f9eb3

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

codext/base/_base2n.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
from ._base import base, _get_charset, BaseError
99

1010

11+
_bin = lambda x: bin(x if isinstance(x, int) else ord(x))
12+
13+
1114
# base en/decoding functions for N a power of 2
1215
class Base2NError(BaseError):
1316
pass
@@ -48,7 +51,7 @@ def base2n_encode(string, charset, errors="strict", exc=Base2NEncodeError):
4851
q += nb_out
4952
# iterate over the characters, gathering bits to be mapped to the charset
5053
for i, c in enumerate(b(string)):
51-
bs += "{:0>8}".format(bin(c)[2:])
54+
bs += "{:0>8}".format(_bin(c)[2:])
5255
while len(bs) >= nb_out:
5356
r += charset[int(bs[:nb_out], 2)]
5457
bs = bs[nb_out:]
@@ -89,7 +92,7 @@ def base2n_decode(string, charset, errors="strict", exc=Base2NDecodeError):
8992
bs += "0" * nb_in
9093
else:
9194
try:
92-
bs += ("{:0>%d}" % nb_in).format(bin(charset.index(c))[2:])
95+
bs += ("{:0>%d}" % nb_in).format(_bin(charset.index(c))[2:])
9396
except ValueError:
9497
if errors == "strict":
9598
raise exc("'base' codec can't decode character '{}' in position {}".format(c, i))

0 commit comments

Comments
 (0)