chiark / gitweb /
Update base91.py so that it can run with Python 2 and Python 3
authorGuillaume Jacquenot <Gjacquenot@users.noreply.github.com>
Mon, 18 May 2015 11:22:05 +0000 (13:22 +0200)
committerGuillaume Jacquenot <Gjacquenot@users.noreply.github.com>
Mon, 18 May 2015 11:22:05 +0000 (13:22 +0200)
base91.py

index 9ce1940499c4b9b2b4e1f67d1130a5a6cbcbbed0..cd279bc18faba3ca9754795ec06b95c65188ede5 100644 (file)
--- 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
-
-
-
-
-
-