How do you know if it is large enough? Well, your current method is almost right. It just needs tweaking. And remember to allow for the minus sign (if required).
It might be simpler to just use a fixed size, say 80 bytes, that should be adequate.
There's no magic there. The original code used a fixed base of 10. I just replaced that with the variable base. When base is 10, the code is more or less doing the same as the original.
So, in order to understand it, you need to understand how it works for one base, such as 10, or maybe 2. When you understand that, the rest is just more of the same.
Okay I see what you are saying, one last thing: What does the n=abs(value) do exactly for this function? I know it takes the absolute value of the value but why do this?
Look at the context. The value input by the user may be negative or positive.
But at line 57 we have while (n>0)
If n is negative, the loop will never execute as it will fail this comparison. So I used that as a somewhat crude way of handling the situation. This isn't necessarily the best approach, as it doesn't mirror exactly the code inside the function good_itoa()
I look at it this way, right now we are interested in the internal workings of this function. But if you think of it as being like the library function (which exists for some compilers, but is not standardised), the user of such a function will treat it as a 'black box' which does something. In practice we would just pass a generously large buffer to the function, without going to the trouble of trying to pre-calculate the exact size.
My preferred approach would be to use a C++ std::string and let the function allocate this for itself, without the user having to be concerned with the size. Though the current code doesn't use any C++ features.
Yes I see exactly what you are saying. I actually thing that is a really smart way of doing this. Thank you for all the help with the code and the extra explanation I really appreciate it.