0

Hello is there a faster way than the one below to convert a bytearray to integer and vice versa in python? I am seeking for a solution which will allow me to speed up the execution time of my scripts.

Thank you.

def bytesToNumber(self,b):
    total = 0
    multiplier = 1
    for count in xrange(len(b)-1, -1, -1):
        byte = b[count]
        total += multiplier * byte
        multiplier *= 256
    return total


def numberToByteArray(self,n, howManyBytes=None):
    if howManyBytes == None:
        howManyBytes = numBytes(n)
    b = bytearray(howManyBytes)
    for count in xrange(howManyBytes-1, -1, -1):
        b[count] = int(n % 256)
        n >>= 8
    return b

0 Answers0