def num_to_bin(num):
    """
    Convert a `num` integer or long to a binary string byte-ordered such that
    the least significant bytes are at the beginning of the string (aka. big
    endian).
    """
    # Zero is not encoded but returned as an empty value
    if num == 0:
        return b'\x00'

    return num.to_bytes((num.bit_length() + 7) // 8, 'big')