strtol / strtoll / strtoul / strtoull
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.\n", 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.\n", 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\n",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.\n", 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 != '\0') 52 { 53 // printf("Further characters after number: %s\n", endptr); 54 } 55 else 56 { 57 isOK = true; 58 } 59 60 return isOK; 61 } 62 63 bool stringToLL(const std::string &str, long long &val) 64 { 65 bool isOK = false; 66 const char *nptr = str.c_str(); 67 char *endptr = NULL; 68 errno = 0; 69 val = strtoll(nptr, &endptr, 10); 70 //error ocur 71 if ((errno == ERANGE && (val == LLONG_MAX || val == LLONG_MIN)) 72 || (errno != 0 && val == 0)) 73 { 74 75 } 76 //no digit find 77 else if (endptr == nptr) 78 { 79 80 } 81 else if (*endptr != '\0') 82 { 83 // printf("Further characters after number: %s\n", endptr); 84 } 85 else 86 { 87 isOK = true; 88 } 89 90 return isOK; 91 } 92 93 bool stringToUL(const std::string &str, unsigned long &val) 94 { 95 bool isOK = false; 96 const char *nptr = str.c_str(); 97 char *endptr = NULL; 98 errno = 0; 99 val = strtoul(nptr, &endptr, 10); 100 //error ocur 101 if ((errno == ERANGE && (val == ULONG_MAX)) 102 || (errno != 0 && val == 0)) 103 { 104 105 } 106 //no digit find 107 else if (endptr == nptr) 108 { 109 110 } 111 else if (*endptr != '\0') 112 { 113 // printf("Further characters after number: %s\n", endptr); 114 } 115 else 116 { 117 isOK = true; 118 } 119 120 return isOK; 121 } 122 123 bool stringToULL(const std::string &str, unsigned long long &val) 124 { 125 bool isOK = false; 126 const char *nptr = str.c_str(); 127 char *endptr = NULL; 128 errno = 0; 129 val = strtoull(nptr, &endptr, 10); 130 //error ocur 131 if ((errno == ERANGE && (val == ULLONG_MAX)) 132 || (errno != 0 && val == 0)) 133 { 134 135 } 136 //no digit find 137 else if (endptr == nptr) 138 { 139 140 } 141 else if (*endptr != '\0') 142 { 143 // printf("Further characters after number: %s\n", endptr); 144 } 145 else 146 { 147 isOK = true; 148 } 149 150 return isOK; 151 }
本文参考自:
http://www.cplusplus.com/reference/cstdlib/strtol/
http://www.cplusplus.com/reference/cstdlib/strtoll/
http://www.cplusplus.com/reference/cstdlib/strtoul/
http://www.cplusplus.com/reference/cstdlib/strtoull/
http://blog.csdn.net/ywy2090/article/details/64918801
posted on 2017-11-28 22:15 LastBattle 阅读(1091) 评论(0) 编辑 收藏 举报