Indeterminate-length big-endian to little-endian conversion in Python
To calculate number of bytes, better use:

numbytes = math.ceil(value.bit_length() / 8)


And then you can just:

def be_to_le(value: int) -&gt; int:
    numbytes = math.ceil(value.bit_length() / 8)
    return int.from_bytes(value.to_bytes(numbytes, byteorder="little"), byteorder="big")