Functions for converting strings to numbers

Source: Internet
Author: User

Similar to atol, atoi, and strtol, the last l represents a long integer, And I represents an int.

Atol (convert string to integer)
Related functions
Atof, atoi, strtodd, strtol, strtoul
Header file
# Include <stdlib. h>
Define functions
Long atol (const char * nptr );
Function Description
Atol () scans the nptr parameter string and skips the leading space character until conversion is started in case of a number or positive or negative sign, the conversion ends only when a non-number or string ends ('/0') and the result is returned.
Return Value
Returns the number of converted long integers.
Additional instructions
Atol () and use strtol (nptr, (char **) null, 10); the results are the same.
Example
/* Convert string a and string B into numbers and add them together */
# Include <stdlib. h>
Main ()
{
Char A [] = "1000000000 ";
Char B [] = "234567890 ";
Long C;
C = atol (A) + atol (B );
Printf ("C = % d/N", c );
}
Running result:
C = 1234567890.




Strtodd (convert a string to a floating point number)
Related functions
Atoi, atol, strtodd, strtol, strtoul
Header file
# Include <stdlib. h>
Define functions
Double strtodd (const char * nptr, char ** endptr );
Function Description
Strtodd () scans the nptr parameter string and skips the leading space character until the conversion starts in case of a number or positive or negative sign, until the occurrence of a non-number or string ends ('/0 ') and return the result. If the endptr value is not null, the character pointer in the nptr that is terminated when an exception occurs is returned by the endptr. The nptr string can contain positive and negative numbers, decimal points, or E (e) to represent the exponent part. Such as 123.456 or 123e-2.
Return Value
Returns the number of floating point types after conversion.
Additional instructions
Refer to atof ().
Example
/* Convert the string A, B, and C into numbers in hexadecimal notation of 10, 2, and 16 */
# Include <stdlib. h>
Mian ()
{
Char A [] = "1000000000 ";
Char B [] = "1000000000 ";
Char C [] = "FFFF ";
Printf ("A = % d/N", strtodd (A, null, 10 ));
Printf ("B = % d/N", strtodd (B, null, 2 ));
Printf ("C = % d/N", strtodd (C, null, 16 ));
}
Running result:
A = 1000000000
B = 512
C = 65535.

 

 

Reference: http://www.17xie.com/read-406473.html

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.