itoa

Convert an integer to an ASCII string

[code lang="cpp"]/* K&R2 60-63 */
char *itoa(int n, char s[])
{
int c, i, j, sign;

if((sign = n) < 0)
n = -n;

do {
s[i++] = n % 10 + '0';
} while((n /= 10) > 0);

if(sign < 0)
s[i++] = '-';

s[i] = '\0';

for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}

return s;
}
[/code]

Related posts:

  1. Binary to Decimal Conversion Converts a binary number to its decimal equivalent. However unlike the bindec function, this will preserve the binary/radix point. Code...

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.