what's the maximum number of characters generated by the following statement? i need to format the output properly.
echo base_convert(sprintf('%u',crc32($_string)),10,36);
what's the maximum number of characters generated by the following statement? i need to format the output properly.
echo base_convert(sprintf('%u',crc32($_string)),10,36);
crc32($_string)
returns the CRC as 32-bit integer whose max value will be 0xFFFFFFFF which in decimal is 4294967295.
sprintf('%u',crc32($_string))
Will return the above value interpreted as unsigned int.
base_convert(sprintf('%u',crc32($_string)),10,36)
This will convert the previously returned int from base 10 to base 36.
Now 4294967295 in base 10 = 1z141z3 in base 36
, which is 7 char long. So looks like the MAX length will be 7 char.
I see that you already have an answer, but I'd like to generalize the solution.
The question is how many base 36 digits are required to represent a number having 32 base 2 digits (i.e. bits). The conversion to/from base 10 in the middle is irrelevant to the problem.
The number of digits is determined by the logarithm of the number in the desired base. We know that the base 2 logarithm is 32, so what's the base 36 logarithm?
32 * log(2) / log(36)
My calculator gives me 6.1896449 or so. Since we can't deal with partial digits, you need to round up to 7. This also explains why 6 digits works most of the time.