Hướng dẫn number base in python

The below provided Python code converts a Python integer to a string in arbitrary base [ from 2 up to infinity ] and works in both directions. So all the created strings can be converted back to Python integers by providing a string for N instead of an integer. The code works only on positive numbers by intention [there is in my eyes some hassle about negative values and their bit representations I don't want to dig into]. Just pick from this code what you need, want or like, or just have fun learning about available options. Much is there only for the purpose of documenting all the various available approaches [ e.g. the Oneliner seems not to be fast, even if promised to be ].

I like the by Salvador Dali proposed format for infinite large bases. A nice proposal which works optically well even for simple binary bit representations. Notice that the width=x padding parameter in case of infiniteBase=True formatted string applies to the digits and not to the whole number. It seems, that code handling infiniteBase digits format runs even a bit faster than the other options - another reason for using it?

I don't like the idea of using Unicode for extending the number of symbols available for digits, so don't look in the code below for it, because it's not there. Use the proposed infiniteBase format instead or store integers as bytes for compression purposes.

    def inumToStr[ N, base=2, width=1, infiniteBase=False,\
    useNumpy=False, useRecursion=False, useOneliner=False, \
    useGmpy=False, verbose=True]:
    ''' Positive numbers only, but works in BOTH directions.
    For strings in infiniteBase notation set for bases 

Chủ Đề