Standards / Extensions | C or C++ | Dependencies |
---|---|---|
z/OS® UNIX | both | z/OS V1R5 |
#define _OPEN_SYS_ITOA_EXT
#include <stdlib.h>
char * itoa(int n, char * buffer, int radix);
(void) sprintf(buffer, "%d", n);
with
buffer the returned character string. When the radix is OCTAL, itoa()
formats integer n into an unsigned octal constant. When the radix
is HEX, itoa() formats integer n into an unsigned hexadecimal constant.
The hexadecimal value will include lower case abcdef, as necessary.String pointer (same as buffer) will be returned. When passed a non-valid radix argument, function will return NULL and set errno to EINVAL.
This is a non-standard function. Even though the prototype given is commonly used by compilers on other platforms, there is no guarantee that this function will behave the same on all platforms, in all cases. You can use this function to help port applications from other platforms, but you should avoid using it when writing new applications, in order to ensure maximum portability.
/* CELEBI12
This example reads an int and formats it to decimal, unsigned
octal, and unsigned hexadecimal constants converted to a
character string.
*/
#define _OPEN_SYS_ITOA_EXT
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int i;
char buffer [sizeof(int)*8+1];
printf ("Enter a number: ");
if (scanf ("%d",&i) == 1) {
itoa (i,buffer,DECIMAL);
printf ("decimal: %s\n",buffer);
itoa (i,buffer,HEX);
printf ("hexadecimal: %s\n",buffer);
itoa (i,buffer,OCTAL);
printf ("octal: %s\n",buffer);
}
return 0;
}
Output
decimal: 1234
hexadecimal: 4d2
octal: 2322