itoa'd you so?

Bryan Sant bryan.sant at gmail.com
Wed Sep 19 16:30:54 MDT 2007


On 9/19/07, Levi Pearson <levi at cold.org> wrote:
> char* itoa( int value, char* result, int base ) {
>   // check that the base if valid
>   if (base < 2 || base > 16) { *result = 0; return result; }
>
>   char* out = result;
>   int quotient = value;
>
>   do {
>     *out = "0123456789abcdef"[ std::abs( quotient % base ) ];
>     ++out;
>     quotient /= base;
>   } while ( quotient );
>
>   // Only apply negative sign for base 10
>   if ( value < 0 && base == 10) *out++ = '-';
>   std::reverse( result, out );
>   *out = 0;
>
>   return result;
> }

This only supports a base of up to 16.  Make me wonder what the limit
is for the actual libc runtime's itoa implementation.  We use a base
59 number in some software I use here at work.  Why 59 and not 64?
Because we've removed the vowels so it's impossible for swear words to
be generated :-).

-Bryan



More information about the PLUG mailing list