x6xtxq
Last Updated: October 13, 2018
·
26.01K
· lsouza

Convert bytes to int or int to bytes in python

def bytes_to_int(bytes):
    result = 0

    for b in bytes:
        result = result * 256 + int(b)

    return result

def int_to_bytes(value, length):
    result = []

    for i in range(0, length):
        result.append(value >> (i * 8) & 0xff)

    result.reverse()

    return result