From ee608b46b5709dfcce5ca741fa87bea883735916 Mon Sep 17 00:00:00 2001 From: Tim Head Date: Mon, 17 Nov 2014 10:45:51 +0100 Subject: [PATCH] Less convoluted conversion of a list of bytes to integer After a few hours of sleep I do remember basic computing --- thesethreewords.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/thesethreewords.py b/thesethreewords.py index a292594..1009266 100644 --- a/thesethreewords.py +++ b/thesethreewords.py @@ -153,12 +153,12 @@ class WordHasher(object): def bytes_to_int(self, bytes): """Convert a list of 6`bytes` to an integer""" assert len(bytes) == 6 - byte_string = [] - for b in bytes: - bs = bin(b)[2:] - bs = "0"*(8-len(bs)) + bs - byte_string.append(bs) - return int(''.join(byte_string), 2) + N = 0 + bytes.reverse() + for n,b in enumerate(bytes): + N += b * (2**(8*(n))) + + return N def to_rugbits(self, integer): """Convert a 45bit `integer` to a list of 3rugbits @@ -173,6 +173,7 @@ class WordHasher(object): def rugbits_to_int(self, rugbits): """Convert a list of `rugbits` to an integer""" + assert len(rugbits) == 3 return (rugbits[0] *(2**30)) + (rugbits[1] *(2**15)) + (rugbits[2])