Less convoluted conversion of a list of bytes to integer

After a few hours of sleep I do remember basic computing
This commit is contained in:
Tim Head 2014-11-17 10:45:51 +01:00
parent 20a256fc65
commit ee608b46b5
1 changed files with 7 additions and 6 deletions

View File

@ -153,12 +153,12 @@ class WordHasher(object):
def bytes_to_int(self, bytes): def bytes_to_int(self, bytes):
"""Convert a list of 6`bytes` to an integer""" """Convert a list of 6`bytes` to an integer"""
assert len(bytes) == 6 assert len(bytes) == 6
byte_string = [] N = 0
for b in bytes: bytes.reverse()
bs = bin(b)[2:] for n,b in enumerate(bytes):
bs = "0"*(8-len(bs)) + bs N += b * (2**(8*(n)))
byte_string.append(bs)
return int(''.join(byte_string), 2) return N
def to_rugbits(self, integer): def to_rugbits(self, integer):
"""Convert a 45bit `integer` to a list of 3rugbits """Convert a 45bit `integer` to a list of 3rugbits
@ -173,6 +173,7 @@ class WordHasher(object):
def rugbits_to_int(self, rugbits): def rugbits_to_int(self, rugbits):
"""Convert a list of `rugbits` to an integer""" """Convert a list of `rugbits` to an integer"""
assert len(rugbits) == 3
return (rugbits[0] *(2**30)) + (rugbits[1] *(2**15)) + (rugbits[2]) return (rugbits[0] *(2**30)) + (rugbits[1] *(2**15)) + (rugbits[2])