Python - 将Bytearray转换为整数

时间:2016-01-03 19:04:49

标签: python bytearray

在python中,Hello是否有比下面更快的方法将bytearray转换为整数,反之亦然?我正在寻找一种解决方案,这将使我加快脚本的执行时间。

谢谢。

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 个答案:

没有答案