Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - What is the function of itoa in C language and how to use it?
What is the function of itoa in C language and how to use it?
Itoa is a widely used non-standard C language extension function. Because it is not a standard C language function, it cannot be used in all compilers. However, most compilers, such as those on Windows, usually run on.

use

char *itoa(int value,char *string,int radix);

Header file:

Program example:

# include & ltstdlib.h & gt

# include & ltstdio.h & gt

int main()

{

Int number =123456;

char string[25];

Itoa (number, string,10);

Printf("integer = %d string = %s\n ",number, string);

Returns 0;

}

/* Source code for implementing itoa function */

char *myitoa(int num,char *str,int radix)

{

/* Index table */

char index[]= " 0 123456789 abcdefghijklmnopqrstuvwxyz ";

Unsigned unum/* intermediate variable *

int i=0,j,k;

/* Determine the value of unum */

if(radix = = 10 & amp; & num<0)/* Decimal negative number */

{

Unum= (unsigned)-num;

str[i++]= '-';

}

Else unum= (unsigned) num/* Other information */

/* Reverse order */

Do {

Str [i++] = index [unsigned% (unsigned) radix];

Unum/= radix;

} while(unum);

str[I]= ' \ 0 ';

/* conversion */

if(str[0]= = '-')k = 1; /* Decimal negative number */

Otherwise k = 0;;

/* Change the original "/2" to "/2.0" to ensure that the correct result can be obtained when num is between 16~255 and radix is equal to 16 */

for(j = k; j & lt=(I- 1)/2.0+k; j++)

{

num = str[j];

str[j]= str[I-j- 1+k];

str[I-j- 1+k]= num;

}

Returns a string;

}

The third parameter of itoa is used to convert numbers into different decimal systems. For example:

# include & ltstdlib.h & gt

# include & ltstdio.h & gt

int main(void)

{

Int number =12345;

char string[25];

Itoa (number, string,10); //Decimal conversion

Printf("integer = %d string = %s\n ",number, string);

Itoa (number, string,16); //Press 16 to convert.

Printf("integer = %d string = %s\n ",number, string);

Returns 0;

}

Output result:

Integer = 12345String = 12345-Description12345 is expressed in decimal as12345.

Integer =12345String = 3039-12345 is expressed in hexadecimal form of 0x3039.

However, it should be noted that itoa is not a standard C function, it is unique to Windows. If you want to write cross-platform programs, please use sprintf.

Let's use decimals:)

Examples of MSDN

example

/* ITOA。 C: This program converts various integers.

* String sizes of various radii.

*/

# include & ltstdlib.h & gt

# include & ltstdio.h & gt

Invalid master (invalid)

{

A charging buffer [20];

int i = 3445

Dragon l =-344115l;

Unsigned length 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 (radix16)": 0x%s\n ",l, buffer);

_ultoa( ul,buffer, 16);

Printf ("unsigned long string %lu (radix 16): 0x%s\n", ul, buffer);

}

output

Integer string 3445 (radix 10): 3445

Integer string 3445 (radix 16): 0xd75

String of integer 3445 (radix 2):1111101

Long string -344 1 15 (radix 16):0x fffabbcd.

Unsigned long integer string 1234567890 (radix 16): 0x499602d2

Specifies the radix of the base to be converted, which seems to be between 1-36.

This is not a function in the C standard library, but an extension under the Windows platform. Sprintf is in the standard library, which is more powerful than this, and its usage is similar to printf:

char str[255];

sprintf(str," %x ", 100); //Converts 100 into a string in hexadecimal notation.