4 ms·
If you were to ask me to write some code to convert an int64 into ascii decimals, I'd start with a 20 character buffer (because int64 will never need more), loo
by billpg 2y ago
If you were to ask me to write some code to convert an int64 into ascii decimals, I'd start with a 20 character buffer (because int64 will never need more), loop to populate the buffer with ascii decimal digits from the lowest significant end, then once the loop has finished and we know how many digits, copy the buffer into its place.
I'd love to told that this method in the article is actually significantly more efficient than my possibly naïve approach.
- matheusmoreira 2y ago> I'd start with a 20 character buffer (because int64 will never need more) Just in case anyone is wondering, this is because the maximum number of decimal digits for a given number of bits is given by: digits(bits) = ceil(bits * log10(2)) This can be generalized to any number of bits. digits(32) = 10 digits(64) = 20 digits(128) = 39 digits(256) = 78 digits(512) = 155 digits(1024) = 309
- Mikhail_Edoshin 2y agoIf you convert an int into a string, then yes, I would do the same. But counting digits alone is also useful (e.g. when aligning many numbers in a column).