From da98a9b32e5932c5af11c1b9a34f47ca746321fb Mon Sep 17 00:00:00 2001
From: Symphorien Gibol <symphorien.gibol@gmail.com>
Date: Fri, 27 May 2016 18:28:24 +0200
Subject: [PATCH] base.py

---
 tunmgr/base.py | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
 create mode 100644 tunmgr/base.py

diff --git a/tunmgr/base.py b/tunmgr/base.py
new file mode 100644
index 0000000..20f709d
--- /dev/null
+++ b/tunmgr/base.py
@@ -0,0 +1,29 @@
+def basetoN(num,base):
+    current=num
+    while current!=0:
+        current, remainder=divmod(current, base)
+        yield remainder
+
+def basefromN(repr, base):
+    n = 0
+    puissance = 1
+    for chiffre in repr:
+        n+=chiffre*puissance
+        puissance*=base
+    return n
+
+def from7bytes(B):
+    n = 0
+    for i, b in enumerate(B):
+        n|=b<<(8*i)
+    return n
+
+def to7bytes(n):
+    for i in range(7):
+        yield (n&(0xff<<(8*i)))>>(8*i)
+
+if __name__=="__main__":
+    print(list(basetoN(12, 2)))
+    print(basefromN(basetoN(12, 2), 2))
+    print(from7bytes(b"abcdefg"))
+    print(bytes(to7bytes(from7bytes(b"abcdefg"))))
-- 
GitLab