Char * ITOA (INT value, char * string, int Radix); int value converted integer, char * string converted character array, int Radix converted hexadecimal number, for example, 2, 8, 10, 16
Hexadecimal header files: <stdlib. h>
ITOA operation and usage
Program example:
# Include <stdlib. h> # include <stdio. h> int main () {int number = 123456; char string [25]; ITOA (number, String, 10 ); printf ("integer = % d string = % s \ n", number, string); Return 0;}/* source code for implementing the ITOA function */char * myitoa (INT num, char * STR, int Radix ){
ITOA Flowchart
/* Index table */
Char index [] = "0123456789 abcdefghijklmnopqrstuvwxyz"; unsigned unum;/* intermediate variable * int I = 0, J, K; /* determine the unum value */If (Radix = 10 & num <0)/* decimal negative number */{unum = (unsigned)-num; STR [I ++] = '-';} else unum = (unsigned) num;/* Other cases * // * Reverse Order */do
C Language Program
{
STR [I ++] = index [unum % (unsigned) Radix]; unum/= Radix;} while (unum); STR [I] = '\ 0 '; /* convert */If (STR [0] = '-') k = 1;/* decimal negative number */else K = 0; char temp; for (j = K; j <= (i-k-1)/2; j ++) {temp = STR [J]; STR [J] = STR [i-j-1]; STR [i-j-1] = temp;} return STR;} The third parameter of ITOA is used to convert numbers into different hexadecimal values. For example: # include <stdlib. h> # include <stdio. h> int main (void) {int number = 12345; char string [25]; ITOA (number, String, 10 ); // convert printf in decimal format ("integer = % d string = % s \ n", number, string); ITOA (number, String, 16 ); // convert printf in hexadecimal notation ("integer = % d string = % s \ n", number, string); Return 0;}. output result: integer = 12345 string = 12345 -- the decimal representation of 12345 is 12345 integer = 12345 string = 3039 -- The hexadecimal representation of 12345 is 0x3039, but note that ITOA is not A standard C function exclusive to Windows. If you want to write cross-platform programs, use sprintf. In a few hexadecimal notation. :) The msdn example is example/* ITOA. c: This program converts Integers of various * sizes to strings in various radixes. */# include <stdlib. h> # include <stdio. h> void 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 10): % s \ n", I, buffer); _ ITOA (I, buffer, 16 ); printf ("string of integer % d (Radix 16): 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 16): 0x % s \ n", l, buffer); _ ultoa (UL, buffer, 16 ); printf ("string of unsigned long % lu (Radix 16): 0x % s \ n", UL, buffer);} outputstring of integer 3445 (Radix 10 ): 3445 string of integer 3445 (Radix 16): 0xd75string of integer 3445 (Radix 2): 110101110101 string of long int-344115 (Radix 16 ): 0 xfffabfcdstring of unsigned long 1234567890 (Radix 16): 0x499602d2 specifies the base number of the base number to be converted. The value seems to be between 1 and 36. This is not a function in the C standard library, it is extended on the Windows platform. The standard library has sprintf, which is more powerful than this one. Its usage is similar to that of printf: Char STR [255]; sprintf (STR, "% x", 100 ); // convert 100 to a hexadecimal string