(n, digits=8, format=DEFAULT_FORMAT)
return s
Convert a python number to a number field.
Convert a python number to a number field.
Convert a python number to a number field .
def itn(n, digits=8, format=DEFAULT_FORMAT):
    """Convert a python number to a number field.
    """
    # POSIX 1003.1-1988 requires numbers to be encoded as a string of
    # octal digits followed by a null-byte, this allows values up to
    # (8**(digits-1))-1. GNU tar allows storing numbers greater than
    # that if necessary. A leading 0o200 or 0o377 byte indicate this
    # particular encoding, the following digits-1 bytes are a big-endian
    # base-256 representation. This allows values up to (256**(digits-1))-1.
    # A 0o200 byte indicates a positive number, a 0o377 byte a negative
    # number.
    if 0 <= n < 8 ** (digits - 1):
        s = bytes("%0*o" % (digits - 1, n), "ascii") + NUL
    elif format == GNU_FORMAT and -256 ** (digits - 1) <= n < 256 ** (digits - 1):
        if n >= 0:
            s = bytearray([0o200])
        else:
            s = bytearray([0o377])
            n = 256 ** digits + n

        for i in range(digits - 1):
            s.insert(1, n & 0o377)
            n >>= 8
    else:
        raise ValueError("overflow in number field")

    return s
def itn ( n , digits = 8 , format = DEFAULT_FORMAT ) : # POSIX 1003.1-1988 requires numbers to be encoded as a string of # octal digits followed by a null-byte, this allows values up to # (8**(digits-1))-1. GNU tar allows storing numbers greater than # that if necessary. A leading 0o200 or 0o377 byte indicate this # particular encoding, the following digits-1 bytes are a big-endian # base-256 representation. This allows values up to (256**(digits-1))-1. # A 0o200 byte indicates a positive number, a 0o377 byte a negative # number. if 0 <= n < 8 ** ( digits - 1 ) : s = bytes ( "%0*o" % ( digits - 1 , n ) , "ascii" ) + NUL elif format == GNU_FORMAT and - 256 ** ( digits - 1 ) <= n < 256 ** ( digits - 1 ) : if n >= 0 : s = bytearray ( [ 0o200 ] ) else : s = bytearray ( [ 0o377 ] ) n = 256 ** digits + n for i in range ( digits - 1 ) : s . insert ( 1 , n & 0o377 ) n >>= 8 else : raise ValueError ( "overflow in number field" ) return s