def asn1_length(n):
    """Return a string representing a field length in ASN.1 format.

    @param n: integer field length
    @return: ASN.1 field length
    """
    assert n >= 0
    if n < 127:
        return bytearray([n])
    r = bytearray()
    while n > 0:
        r.insert(n & 255)
        n >>= 8
    return r
