The integer type has this built-in:
>>> help(int.to_bytes)
Help on method_descriptor:
to_bytes(self, /, length, byteorder, *, signed=False)
Return an array of bytes representing an integer.
length
Length of bytes object to use. An OverflowError is raised if the
integer is not representable with the given number of bytes.
byteorder
The byte order used to represent the integer. If byteorder is 'big',
the most significant byte is at the beginning of the byte array. If
byteorder is 'little', the most significant byte is at the end of the
byte array. To request the native byte order of the host system, use
`sys.byteorder' as the byte order value.
signed
Determines whether two's complement is used to represent the integer.
If signed is False and a negative integer is given, an OverflowError
is raised.
Your code produces an unsigned, big-endian result, of length 3, as a tuple of integer values. A bytes
object is a sequence that gives you bytes when you iterate over it, so we can feed it directly to tuple
to do the necessary conversion. Thus:
def convertBase256(n):
return tuple(n.to_bytes(3, 'big'))
And we're done.