Any C language type (integer, long integer, floating point, etc.) into a string of digital Itoa ()

1, C language standard library provides several functions, of any type (integer, long integer, floating point, etc.) can be converted into a digital string, the following method and listed in the description of each function.
● itoa (): the integer value into a string.
● ltoa (): The long integer value to a string.
● ultoa (): unsigned long integer value to a string.
● gcvt (): converting the floating-point number to a string, to take rounded.
● ecvt (): double precision floating point value is converted to a string, the conversion results do not contain a decimal point.
● fcvt (): Specifies the number of digits for the conversion accuracy, and the rest with ecvt ().


Further in addition, it may also be used sprintf series digital function into a string, which ratio Itoa () function is running slow series

 

itoa (): the integer value into a string.

/* itoa:  convert n to characters in s */
 void itoa(int n, char s[])
{
    int i, sign;

    if ((sign = n) < 0)  /* record sign */
        n = -n;          /* make n positive */
    i = 0;
    do {       /* generate digits in reverse order */
        s[i++] = n % 10 + '0';   /* get next digit */
    } while ((n /= 10) > 0);     /* delete it */
    if (sign < 0)
        s[i++] = '-';
    s[i] = '\0';
    reverse(s);
}

Use examples:

int a=956;   
itoa( a, 	temp_buf );	
printf_string(temp_buf);

 

 

 

2, C / C ++ language provides several standard library function, it can be converted into strings of any type (integer, long integer, floating point, etc.).

● atof (): Converts a string to a double precision floating point value.
● atoi (): Converts a string to an integer value.
● atol (): Converts a string to a long value.
● strtod (): Converts a string to a double precision floating point value, and report all of the remaining numbers are not converted.
● strtol (): the value into a long string, and to report all of the remaining digits can not be converted.
● strtoul (): converts a string to an unsigned long integer value, and report all of the remaining digits can not be converted

 

 

 

Reference: https://www.cnblogs.com/bluestorm/p/3168719.html

Published 162 original articles · won praise 125 · views 470 000 +

Guess you like

Origin blog.csdn.net/jiangchao3392/article/details/100653170