import six


def long_to_bytes(number, length=0):
    """Convert a long to a byte string, padded with \x00 to `length`"""
    byteslist = bytes()
    while number:
        value, number = number & 0xff, number >> 8
        byteslist = six.int2byte(value) + byteslist

    if length and length > len(byteslist):
        pad = length - len(byteslist)
        byteslist = six.b("\x00" * pad) + byteslist
    elif length and length < len(byteslist):
        byteslist = byteslist[:length]
    return byteslist