I'm converting the following string to it's unsigned integer representation:
str = '\x00\x00\x00\x00\x00\x00\x01\xFF'
I can use struct.unpack('8B', str)
to get the tuple representation (0,0,0,0,0,0,1,255)
, but what's the quickest/easiest way to convert this tuple to an int?
Right now, my code is
def unpack_str(s):
i = r = 0
for b in reversed(struct.unpack('8B', s)):
r += r*2**i
i++
return r
But this is long and ugly, for such a simple function! There must be a better way! Can any SO python gurus help me to trim this down and python-ify it?
i++
is invalid syntax (as opposed to the equally meaningless, but more sneaky++i
which runs but doesn't do anything).