C-Library-----String (string) and integer (int), floating-point (float), etc. conversion

Source: Internet
Author: User
Tags sprintf

#include <stdlib.h>

The 1.int/float to STRING/ARRAY:C language provides several standard library functions that convert numbers of any type (integer, long integer, floating-point type, etc.) to strings, and the methods and descriptions of each function are listed below.
Itoa (): Converts an integer value to a string.
Ltoa (): Converts a long integer value to a string.
Ultoa (): Converts an unsigned long integer value to a string.
GCVT (): Converts floating-point numbers to strings, rounded.
ECVT (): Converts a double-precision floating-point value to a string that does not contain a decimal point in the transformation result.
FCVT (): The specified number of digits is the conversion precision and the remainder is ECVT ().
In addition, you can use the SPRINTF series function to convert numbers to strings that run slower than itoa () series functions

2. String/array to Int/float
C + + language provides several standard library functions that can convert strings to any type (integer, long, floating-point, and so on). 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 integer value.
Strtod (): Converts a string to a double-precision floating-point value and reports all remaining digits that cannot be converted.
Strtol (): Converts a string to a long integer value and reports all remaining digits that cannot be converted.
Strtoul (): Converts a string to an unsigned long integer value and reports all remaining digits that cannot be converted.

The following is an example of converting an integer to a string using the Itoa () function:
# include <stdio.h>
# include <stdlib.h>
void Main (void)
{
int num = 100;
Char str[25];
ITOA (num, str, 10);
printf (' The number ' num ' is%d and the string ' str ' is%s. \ n ',
num, str);
}

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 transfer the number. In the example above, 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.