From Gardenology.org - Plant Encyclopedia and Gardening Wiki
Jump to navigationJump to search
4,465 bytes added
, 05:33, 23 November 2007
{{lowercase|title=itoa}}
The '''itoa''' function is a widespread non-standard extension to the standard [[C (programming language)|C programming language]]. It cannot be portably used, as it is not defined in any of the C language standards; however, compilers often provide it through the header <[[stdlib.h]]> while in non-conforming mode, because it is a logical counterpart to the standard library function <code>[[atoi]]</code>.
:<code>void itoa(int input, char *buffer, int radix)</code>
<code>itoa</code> takes the integer input value <code>input</code> and converts it to a number [[base (mathematics)|in base]] <code>radix</code>. The resulting number (a sequence of base-<code>radix</code> digits) is written to the output buffer <code>buffer</code>.
Depending on the implementation, <code>itoa</code> may return a pointer to the first character in <code>buffer</code>, or may be designed so that passing a [[null pointer|null]] <code>buffer</code> causes the function to return the length of the string that ''would have'' been written into a valid <code>buffer</code>.
For converting a number to a string in base 8 (octal), 10 (decimal), or 16 ([[hexadecimal]]), a Standard-compliant alternative is to use the [[C standard library|standard library]] function <code>[[sprintf]]</code>.
==K&R implementation==
The function <code>itoa</code> appeared in the first edition of [[Brian Kernighan|Kernighan]] and [[Dennis Ritchie|Ritchie]]'s ''[[The C Programming Language (book)|The C Programming Language]]'', on page 60. The second edition of ''[[The C Programming Language (book)|The C Programming Language]]'' ("K&R2") contains the following implementation of <code>itoa</code>, on page 64. The book notes several issues with this implementation, including the fact that it does not correctly handle the [[Two's complement#The_weird_number|weird number]] −2<sup>wordsize-1</sup>.<ref>For the solution to this exercise, see [http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_3:Exercise_4 "K&R2 solutions"] on ''clc-wiki.net''.</ref>
<code lang="c">
/* itoa: convert n to characters in s */
void itoa(int n, char s[])
{
int i, sign;
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
</code>
The function <code>reverse</code> is implemented two pages earlier:
<code lang="c">
#include <string.h>
/* reverse: reverse string s in place */
void reverse(char s[])
{
int c, i, j;
for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
</code>
An <code>itoa</code> function (and a similar function, <code>ftoa</code>, that converted a [[floating point|float]] to a string) is listed in the first-edition [[Unix]] manual.<ref>[http://cm.bell-labs.com/cm/cs/who/dmr/1stEdman.html "Unix Programmer's Manual"], November 3, 1971. Section [http://cm.bell-labs.com/cm/cs/who/dmr/pdfs/man31.pdf "Library routines"].</ref> Unlike the versions given above, the Unix library version had an interface roughly equivalent to
:<code>void itoa(int input, void (*subr)(char))</code>
and would invoke the [[callback (computer science)|callback routine]] <code>subr</code> on each character in the output string, thus eliminating the need for a buffer big enough to hold the entire string.
==See also==
*[[atoi]]
==References==
<references/>
==External links==
*[http://www.freebookzone.com/others/itoa.h A simple itoa function] - bad example with memory leak
*[http://www.freebookzone.com/others/itoa.c itoa example file]
*[http://www.jb.man.ac.uk/~slowe/cpp/itoa.html itoa() implementations with performance tests]
*[http://boost.org/libs/conversion/lexical_cast.htm lexical_cast] - C++ alternative, part of the boost libraries
*[http://code.google.com/p/stringencoders/wiki/NumToA modp_numtoa] - C/C++ alternative for converting integers and floats to char buffers. 5-20x faster than using sprintf
*[http://sourceforge.net/projects/itoa/ Good old Integer To Ascii conversion: itoa] - Another fast implementation of itoa for various datatypes, plus some boost-style wrapping in the form of boost::lexical_cast template specializations.
[[Category:Stdlib.h]]