Please note that your NumToStr() function in your Python code is incorrect due to a dropdown on your base, it should be:
def NumToStr(num): str='' while num: str+=alpha[(num)%base] num=(num)/base return ''.join(list(reversed(str)))
Note that checking that this function returns the correct result would find an error (for example, use alpha="0123456789" ).
With this fix, we get the correct number of digits using this formula:
nDigits = int(ceil(log(nmb, base)))
except for the exact power of the base ( base**0 , base**1 , base**2 , etc.), where it is exactly one less than it should be. This can be fixed by slightly changing the forum:
nDigits = 1 + floor(log(nmb, base))
Note that even this may seem unsuccessful for some inputs (for example, converting from base-10 to base-10 incorrectly indicates 3 digits for 1000 and 6 digits for 1,000,000). This is apparently due to the inprecision float inherent, for example:
print floor(log(1000, 10))
outputs 2 instead of the expected 3 .
As for your mention of performance, I wouldn't worry about performance issues for such trivial code until you have done the profiling / benchmarking, which shows that this is a problem. For example, your "very slow" C code will occupy no more than 38 divisions by 10 for a 128-bit number. If you need better performance than this, you will run into the same problem with any trivial method mentioned here. The fastest thing I can think of is the regular log() function using a combination of a lookup table and linear interpolation, but you have to be careful with the accuracy you get.