From a7803a7906e7ed0035d0779ff781c172348bdac1 Mon Sep 17 00:00:00 2001 From: Guillaume Jacquenot Date: Mon, 18 May 2015 13:22:05 +0200 Subject: [PATCH] Update base91.py so that it can run with Python 2 and Python 3 --- base91.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/base91.py b/base91.py index 9ce1940..cd279bc 100644 --- a/base91.py +++ b/base91.py @@ -1,6 +1,7 @@ -# Base91 encode/decode +# Base91 encode/decode for Python 2 and Python 3 # # Copyright (c) 2012 Adrien Beraud +# Copyright (c) 2015 Guillaume Jacquenot # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -64,14 +65,14 @@ def decode(encoded_str): if v+1: out += struct.pack('B', (b | v << n) & 255 ) return out - + def encode(bindata): ''' Encode a bytearray to a Base91 string ''' - l = len(bindata) b = 0 n = 0 out = '' - for byte in bindata: + for count in range(len(bindata)): + byte = bindata[count:count+1] b |= struct.unpack('B', byte)[0] << n n += 8 if n>13: @@ -83,15 +84,9 @@ def encode(bindata): v = b & 16383 b >>= 14 n -= 14 - out += base91_alphabet[v % 91] + base91_alphabet[v / 91] + out += base91_alphabet[v % 91] + base91_alphabet[v // 91] if n: out += base91_alphabet[b % 91] if n>7 or b>90: - out += base91_alphabet[b / 91] + out += base91_alphabet[b // 91] return out - - - - - - -- 2.30.2