Dec 2007
A mostly useless function
The following definition calculates the number of decimal digits needed to represent a binary number with n digits:

#define COUNT_DECIMAL_DIGITS_BITS(n) ((n * 30103 + 99993) / 100000)

It's accurate for values of n in the range 0 <= n < 39903 (99993 is not a mistake, while 99999 is the more correct round-up value overall, it produces two incorrect values for n less than 39903). The equivalent floating point function is:

double decimalDigits(double n) {return ceil(log10(pow(2, n)));}

or better:

double decimalDigits(double n) {return ceil(n * log10(2));}

To count the number of decimal digits needed to represent a binary number consisting of n bytes:

#define COUNT_DECIMAL_DIGITS_BYTES(n) ((n * 30103 + 12499) / 12500)

It's accurate for values of n in the range 0 <= n < 12767.

Why is this function mostly useless rather than entirely useless? It can be used to compute the minimum size of the resulting string when converting an integer to a string:

#define COUNT_DECIMAL_DIGITS_TYPE(type) ((sizeof(type) * 30103 + 12499) / 12500)

unsigned number = rand();
char digits[COUNT_DECIMAL_DIGITS_TYPE(number) + 1];
sprintf(digits, "%ud", number);

Of course, the number of bytes in an integral type is unlikely to grow beyond 16 (128 bits) or at most 32 (256 bits) in any computer I will ever use.