Convert an integer to a string using a given base
#include <stdlib.h>
char* itoa( int value,
char* buffer,
int radix );
libc
The itoa() function converts the integer value into the equivalent string in base radix notation, storing the result in the specified buffer. The string will be NUL-terminated.
The size of buffer must be at least
8 * sizeof( int ) + 1
bytes when converting values in base 2 (binary).
If the value of radix is 10, and value is negative, then a minus sign is prepended to the result.
A pointer to the resulting string.
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
char buffer[20];
int base;
for( base = 2; base <= 16; base += 2 ) {
printf( "%2d %s\n", base,
itoa( 12765, buffer, base ) );
}
return EXIT_SUCCESS;
}
produces the output:
2 11000111011101 4 3013131 6 135033 8 30735 10 12765 12 7479 14 491b 16 31dd
| Safety: | |
|---|---|
| Cancellation point | No |
| Interrupt handler | Yes |
| Signal handler | Yes |
| Thread | Yes |
atoi(), atol(), ltoa(), sscanf(), strtol(), strtoul(), ultoa(), utoa()