Converts a string to a long int value.

Source: Internet
Author: User

 
 
Converts a string to a long int value.
Int strToInteger (string s)
{
// S. c_str (): converts a string object to a char * object and returns the const char * type;
// Strtol: converts a string to a long int. The value 16 indicates that the valid character accepted is '0 '~ '9', 'A '~ 'F ';
Char * p;
Long n = strtol (s. c_str (), & p, 16 );
If (* p) = NULL)
{
Printf ("invalid input data \ n ");
}
Return n;
}
Function:
Long int strtol (const char * nptr, char ** endptr, int base );
This function converts the nptr parameter to the Growth integer number based on the base parameter. The base parameter ranges from 2 to 36, or 0. The base parameter indicates the base mode. For example, if the base value is 10, the base value is 10. If the base value is 16, the base value is hexadecimal. When the base value is 0, 10 is used for conversion, however, if the '0x 'prefix is used for hexadecimal conversion, and if the '0' prefix is not '0x', the '8' prefix is used for conversion. At the beginning, strtol () scans the nptr parameter string and skips the leading space character until it starts to convert in case of a number or positive or negative sign, when a non-number or string ends ('\ 0'), the conversion ends and the result is returned. If the endptr parameter is not NULL, the character pointer in the nptr that is terminated due to an exception is returned by endptr. If the endptr parameter is NULL, no invalid string is returned.
 
This is mainly reflected in the following aspects:
1. Not only can recognize decimal integers, but also other hexadecimal integers, depending on the base parameter, such as strtol ("0XDEADbeE ~~ ", NULL, 16) returns the 0xdeadbee value, strtol (" 0777 ~~ ", NULL, 8) returns the value of 0777.
2. endptr is an outgoing parameter. The function returns the first character that is not recognized. For example, char * pos; strtol ("123abc", & pos, 10);, strtol returns 123, and pos points to the letter a in the string. If no identifiable integer exists at the beginning of the string, for example, char * pos; strtol ("ABCabc", & pos, 10);, strtol returns 0, and pos points to the beginning of the string, this can be used to determine the case of this error, which is not handled by atoi.
3. if the integer in the string exceeds the long int value range (overflow or underflow), strtol returns the maximum (or least) integer it can represent and sets errno to ERANGE, for example, strtol ("0XDEADbeef ~~ ", NULL, 16) returns 0x7fffffff and sets errno to ERANGE.
Example from msdn:
# Include <stdlib. h>
# Include <stdio. h>
Void main (void)
{
Char * string, * stopstring;
Double x;
Int base;
Long l;
Unsigned long ul;
String = "3.1415926 This stopped it ";
X = strtodd (string, & stopstring );
Printf ("string = % s \ n", string );
Printf ("strtodd = % f \ n", x );
Printf ("Stopped scan at: % s \ n", stopstring );
String = "-10110134932 This stopped it ";
L = strtol (string, & stopstring, 10 );
Printf ("string = % s", string );
Printf ("strtol = % ld", l );
Printf ("Stopped scan at: % s", stopstring );
String = "10110134932 ";
Printf ("string = % s \ n", string );
/* Convert string using base 2, 4, and 8 :*/
For (base = 2; base <= 8; base * = 2)
{
/* Convert the string :*/
Ul = strtoul (string, & stopstring, base );
Printf ("strtol = % ld (base % d) \ n", ul, base );
Printf ("Stopped scan at: % s \ n", stopstring );
}
}
Output result:
String = 3.1415926 This stopped it
Strtodd = 3.141593
Stopped scan at: This stopped it
String =-10110134932 This stopped it
Strtol =-2147483648 Stopped scan at: This stopped it
String = 10110134932
Strtol = 45 (base 2)
Stopped scan at: 34932
Strtol = 4423 (base 4)
Stopped scan at: 4932
Strtol = 2134108 (base 8)
Stopped scan at: 932

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.