def asn1_length(n):
    """Return a string representing a field length in ASN.1 format."""
    assert n >= 0
    if n < 0x7f:
        return chr(n)
    r = ""
    while n > 0:
        r = chr(n & 0xff) + r
        n >>= 8
    return r