The prototype of the ITOA () function is: char *itoa (int value, char *string,int radix);
The itoa () function has 3 parameters: The first argument is the number to convert, the second parameter is the target string to write the transformation result, and the third parameter is the cardinality used to convert the number. In the example, the conversion base is 10. 10: decimal; 2: Binary ...
Itoa is not a standard C function, it is Windows specific, if you want to write Cross-platform program, please use sprintf.
is extended under the Windows platform, with sprintf in the standard library, more powerful than this, with a similar usage to printf:
Char str[255];
sprintf (str, "%x", 100); A string that converts 100 to a 16-binary representation.
The following is a decimal octal method:
Copy Code code as follows:
#include "stdio.h"
#include "Stdlib.h"
int main (void)
{
int num = 10;
Char str[100];
ITOA (num, str, 8); Converts integer 10 to octal to save in a str character array
printf ("%s\n", str);
System ("pause");
return 0;
}
The following is a decimal conversion method:
Copy Code code as follows:
#include "stdio.h"
#include "Stdlib.h"
int main (void)
{
int num = 15;
Char str[100];
int n = atoi (itoa (num, str, 2)); Converts num to a binary string, then converts the string to an integer
printf ("%d\n", N);
System ("pause");
return 0;
}
Extension of the itoa () function:
Copy Code code as follows:
char *_itoa (int value, char *string, int radix);
Char *_i64toa (__int64 value, char *string, int radix);
char * _UI64TOA (unsigned _int64 value, char *string, int radix);
wchar_t * _itow (int value, wchar_t *string, int radix);
wchar_t * _i64tow (__int64 value, wchar_t *string, int radix);
wchar_t * _ui64tow (unsigned __int64 value, wchar_t *string, int radix);
The program code is as follows:
Copy Code code as follows:
#include "stdio.h"
#include "Stdlib.h"
int main (void)
{
Char buffer[20];
int i = 3445;
Long L = -344115l;
unsigned long ul = 1234567890UL;
_itoa (i, buffer, 10);
printf ("String of Integer%d (radix):%s\n", I, buffer);
_itoa (i, buffer, 16);
printf ("String of Integer%d (radix): 0x%s\n", I, buffer);
_itoa (i, buffer, 2);
printf ("String of Integer%d (radix 2):%s\n", I, buffer);
_ltoa (l, buffer, 16);
printf ("String of long int%ld (radix): 0x%s\n", L,buffer);
_ULTOA (UL, buffer, 16);
printf ("String of unsigned Long%lu (radix): 0x%s\n", Ul,buffer);
System ("pause");
return 0;
}