itoa'd you so?

Levi Pearson levi at cold.org
Wed Sep 19 15:11:07 MDT 2007


Weston Cerny <weston at aircomusa.com> writes:

> Oops i'm double using my variable and not checking my bounds correctly
> for a negative number.
>
> string itoa(int in) {
>   string ret = "";
>   int current = in;
>   int digit = 0;
>
>   while (current != 0 && digit = current % 10)
>   {
>      ret = (char)(digit+ (int)'0') + ret;
>      current /= 10;
>   }
>
>   return ret;
> }

I'm sure this is much closer to what they were looking for.  On the
other hand, strings are not a native datatype, so you're still using
standard libraries.  On many embedded systems, you may not have the
luxury of a standard string class, so you'd be stuck with arrays of
characters.  This makes it a little more tricky, since you now have to
deal with the size of the array, who allocates it, etc.

                --Levi



More information about the PLUG mailing list