function
long int strtol (const char* str, char** endptr, int base); —— Convert string to long integer
long long int strtoll (const char* str, char** endptr, int base); —— Convert string to long long integer
unsigned long int strtoul (const char* str, char** endptr, int base); —— Convert string to unsigned long integer
unsigned long long int strtoull (const char* str, char** endptr, int base); —— Convert string to unsigned long long integer
Parses the C-string str interpreting its content as an integral number of the specified base, which is returned as a long int
/ long long int
/ unsigned long int /
value. If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number.unsigned long long int
The function first discards as many whitespace characters as necessary
until the first non-whitespace character is found. Then, starting from
this character, takes as many characters as possible that are valid
following a syntax that depends on the base parameter, and
interprets them as a numerical value. Finally, a pointer to the first
character following the integer representation in str is stored in the object pointed by endptr.
If the value of base is zero, the syntax expected is similar to that of integer constants, which is formed by a succession of:
- An optional sign character (
+
or-
) - An optional prefix indicating octal or hexadecimal base (
"0"
or"0x"/"0X"
respectively) - A sequence of decimal digits (if no base prefix was specified) or either octal or hexadecimal digits if a specific prefix is present
If the base value is between 2 and 36, the format expected
for the integral number is a succession of any of the valid digits
and/or letters needed to represent integers of the specified radix
(starting from '0'
and up to 'z'
/'Z'
for radix 36). The sequence may optionally be preceded by a sign (either +
or -
) and, if base is 16, an optional "0x"
or "0X"
prefix.
If the first sequence of non-whitespace characters in str is not a valid integral number as defined above, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
For locales other than the "C"
locale, additional subject sequence forms may be accepted.
Parameters
- str
- C-string beginning with the representation of an integral number.
- endptr
- Reference to an object of type
char*
, whose value is set by the function to the next character in str after the numerical value.
This parameter can also be a null pointer, in which case it is not used. - base
- Numerical base (radix) that determines the valid characters and their interpretation.
If this is0
, the base used is determined by the format in the sequence (see above).
Return Value
On success, the function returns the converted integral number as a long int
/ long long int
/ unsigned long int
/ unsigned long long int
value.
If no valid conversion could be performed, a zero value is returned (0L /
).0LL
/
/ 0UL
0ULL
If the value read is out of the range of representable values by a long int /
, the function returnsLONG_MAX orLONG_MIN /LLONG_MAX orLLONG_MIN / ULONG_MAX /ULLONG_MAX (defined in<climits>), and errno is set to ERANGE.long long int
/ unsigned long int
/ unsigned long long int
examples
1 /* strtol example */ 2 #include <stdio.h> /* printf */ 3 #include <stdlib.h> /* strtol */ 4 5 int main () 6 { 7 char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff"; 8 char * pEnd; 9 long int li1, li2, li3, li4; 10 li1 = strtol (szNumbers,&pEnd,10); 11 li2 = strtol (pEnd,&pEnd,16); 12 li3 = strtol (pEnd,&pEnd,2); 13 li4 = strtol (pEnd,NULL,0); 14 printf ("The decimal equivalents are: %ld, %ld, %ld and %ld. ", li1, li2, li3, li4); 15 return 0; 16 }
运行结果:
The decimal equivalents are: 2001, 6340800, -3624224 and 7340031
1 /* strtoll example */ 2 #include <stdio.h> /* printf, NULL */ 3 #include <stdlib.h> /* strtoll */ 4 5 int main () 6 { 7 char szNumbers[] = "1856892505 17b00a12b -01100011010110000010001101100 0x6fffff"; 8 char* pEnd; 9 long long int lli1, lli2, lli3, lli4; 10 lli1 = strtoll (szNumbers, &pEnd, 10); 11 lli2 = strtoll (pEnd, &pEnd, 16); 12 lli3 = strtoll (pEnd, &pEnd, 2); 13 lli4 = strtoll (pEnd, NULL, 0); 14 printf ("The decimal equivalents are: %lld, %lld, %lld and %lld. ", lli1, lli2, lli3, lli4); 15 return 0; 16 }
运行结果:
The decimal equivalents are: 1856892505, 6358606123, -208340076 and 7340031
1 /* strtoul example */ 2 #include <stdio.h> /* printf, NULL */ 3 #include <stdlib.h> /* strtoul */ 4 5 int main () 6 { 7 char buffer [256]; 8 unsigned long ul; 9 printf ("Enter an unsigned number: "); 10 fgets (buffer, 256, stdin); 11 ul = strtoul (buffer, NULL, 0); 12 printf ("Value entered: %lu. Its double: %lu ",ul,ul*2); 13 return 0; 14 }
运行结果:
Enter an unsigned number: 30003 Value entered: 30003. Its double: 60006
1 /* strtoull example */ 2 #include <stdio.h> /* printf, NULL */ 3 #include <stdlib.h> /* strtoull */ 4 5 int main () 6 { 7 char szNumbers[] = "250068492 7b06af00 1100011011110101010001100000 0x6fffff"; 8 char * pEnd; 9 unsigned long long int ulli1, ulli2, ulli3, ulli4; 10 ulli1 = strtoull (szNumbers, &pEnd, 10); 11 ulli2 = strtoull (pEnd, &pEnd, 16); 12 ulli3 = strtoull (pEnd, &pEnd, 2); 13 ulli4 = strtoull (pEnd, NULL, 0); 14 printf ("The decimal equivalents are: %llu, %llu, %llu and %llu. ", ulli1, ulli2, ulli3, ulli4); 15 return 0; 16 }
运行结果:
The decimal equivalents are: 250068492, 2064035584, 208622688 and 7340031.
封装
1 bool stringToI32(const std::string &str, int32_t &val) 2 { 3 long temp_val = 0; 4 bool isOK = stringToL(str, temp_val); 5 val = temp_val; 6 return isOK && (temp_val >= -0x7fffffff && temp_val <= 0x7fffffff/*32bit整形的有效范围*/); 7 } 8 9 bool stringToU32(const std::string &str, uint32_t &val) 10 { 11 unsigned long temp_val = 0; 12 bool isOK = stringToUL(str, temp_val); 13 val = temp_val; 14 return isOK && (temp_val <= 0xffffffff/*32bit无符号整形的有效范围*/); 15 } 16 17 bool stringToI64(const std::string &str, int64_t &val) 18 { 19 long long temp_val = 0; 20 bool isOK = stringToLL(str, temp_val); 21 val = temp_val; 22 return isOK && (temp_val >= -0x7fffffffffffffff && temp_val <= 0x7fffffffffffffff/*64bit整形的有效范围*/); 23 } 24 25 bool stringToU64(const std::string &str, uint64_t &val) 26 { 27 unsigned long long temp_val = 0; 28 bool isOK = stringToULL(str, temp_val); 29 val = temp_val; 30 return isOK && (temp_val <= 0xffffffffffffffff/*64bit无符号整形的有效范围*/); 31 } 32 33 bool stringToL(const std::string &str, long &val) 34 { 35 bool isOK = false; 36 const char *nptr = str.c_str(); 37 char *endptr = NULL; 38 errno = 0; 39 val = strtol(nptr, &endptr, 10); 40 //error ocur 41 if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) 42 || (errno != 0 && val == 0)) 43 { 44 45 } 46 //no digit find 47 else if (endptr == nptr) 48 { 49 50 } 51 else if (*endptr != '