1 #include <iostream> 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 int main() 6 { 7 char szInput[256]; 8 unsigned long ul; 9 printf("please input a number:\n"); 10 fgets (szInput, 256, stdin); 11 ul = strtoul (szInput, NULL, 0); 12 printf("Value entered: %lu. Its double: %lu\n", ul, ul*2); 13 return 0; 14 }
输出结果:
1 please input a number: 2 56788 3 Value entered: 56788. Its double: 113576
strtoul的用法:
unsigned long int strtoul ( const char * str, char ** endptr, int base );
第一个参数 const char *str: 源字符数组str, 可以是cstring类型(字符数组), 也可以是c++ string类型,例如 strtoul("4543345", );
第二个参数 char ** endptr: 若参数endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr返回;若参数endptr为NULL,则会不返回非法字符串。还有一点是读入参数1的第一个数字字符之前,之前的空格符都会被忽略,而不看作是非法字符,并记录
其地址返回给参数 endptr...,比如 str = " 3434 111", 3434之前的空格不看作是非法字符,而之后的第一个空格看作为错误的非法字符,并返回地址给endptr... 而记录的地址endptr可以作为下一轮读取111的参数1使用,同样读取111前的空格被忽略,
而不作为非法字符提示...
第三个参数 int base 表示读入字符串(第一个参数 char * str)按照什么进制来读去,比如 2就按照2进制。参数base范围从2至36,或0。参数base代表采的进制方式,如base值为8则采用8进制读取str,若base值为16则采用16进制读取str等。
函数返回值: 为从第一个字符串参数str读出的符合数字字符规范的 unsigned long类型数值...
当base 值为0时或者10时则是采用10进制做转换,但遇到如’0x’前置字符则会使用16进制做转换、遇到’0’前置字符而不是’0x’的时候会使用8进制做转换。
如 源字符数组为:123456ff789 因为f是非数字字符,所以由第二个参数endptr返回f的地址。
如果第三个参数为0时,会根据第一个参数str的前缀来判断进制,如果str中以0x开头,则自动由16进制转换为10进制作为返回值,如果str中以0开头,则自动由8进制转换为10进制
作为返回值,举例:
当第三个参数base为0时,会根据第一个输入源进行必要的进制转换,如果参数base指定10而不是0,则不会根据第一个参数的0x,0进行16或者8进制至10进制的转换!
1 #include <iostream> 2 using namespace std; 3 int main(int argc ,char* argv[]) 4 { 5 char* erjinzhi = "0x134a"; //当第三个参数base为0时,会根据第一个输入源进行必要的进制转换,如果参数base指定10而不是0,则不会根据第一个参数的0x,0进行16或者8进制的转换! 6 char* p; 7 int hextodec = strtoul(erjinzhi, &p, 0); //由16进制转为10进制 8 char * test = "0123"; 9 int octodec = strtoul(test, NULL, 0); //由8进制转为10进制 10 cout<< "hex to dec:" <<endl; 11 cout<< hextodec <<endl; 12 cout<< *p <<endl; 13 14 cout<< "oc to dec:" <<endl; 15 cout<< octodec <<endl; 16 17 return 0; 18 } 19 输出结果: 20 hex to dec: 21 4938 22 23 oc to dec: 24 83
举例返回第二个参数 endptr的例子:
1 #include <iostream> 2 using namespace std; 3 int main(int argc ,char* argv[]) 4 { 5 char* erjinzhi="1111a123"; 6 char* p; 7 int shijinzhi=strtoul(erjinzhi,&p,2); //取指针的地址,即 char ** 8 cout<<shijinzhi<<endl; 9 cout<<*p<<endl; //输出的是a,即第二个参数p保存的是第一个不符合数字字符的地址... 10 return 0; 11 } 12 13 输出: 14 15 15 a
举例:第三个参数的例子:
1 #include <iostream> 2 using namespace std; 3 int main(int argc ,char* argv[]) 4 { 5 char* erjinzhi="13411a123"; 6 char* p; 7 int shijinzhi=strtoul(erjinzhi,&p,2); //因为第三个参数指定了erjinzhi按照二进制来读,所以读入的结果只有1,第二位开始3已经不符合二进制的要求,所以第二个参数指针所指的也是3的地址... 8 cout<<shijinzhi<<endl; 9 cout<<*p<<endl; 10 return 0; 11 } 12 13 输出结果: 14 1 15 3
Parses the C string str interpreting its content as an unsigned integral number of the specified base, which is returned as an unsigned long int value.
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 prefix indicating octal or hexadecimal base ("0" or "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 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). If the base is 16, the sequence may optionally be preceded by "0x" or "0X", which is ignored.
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.
Parameters
-
str
- C string containing the representation of an unsigned 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.
Return Value
On success, the function returns the converted integral number as a long int value.
If no valid conversion could be performed, a zero value is returned.
If the correct value is out of the range of representable values, ULONG_MAX is returned, an the global variable errno is set to ERANGE. Notice that ULONG_MAX is also a valid in-bound return value (in this case errno is not modified by strtoul).
****************************
strtol类似于strtoul: long int strtol ( const char * str, char ** endptr, int base );
举例:
1 /* strtol example */ 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <iostream> 5 using namespace std; 6 7 int main () 8 { 9 char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff"; 10 char * pEnd; 11 long int li1, li2, li3, li4; 12 li1 = strtol (szNumbers,&pEnd,10); 13 cout<<"endptr of li1 is: "<<&pEnd<<"its value is: "<<*pEnd<<endl; 14 li2 = strtol (pEnd,&pEnd,16); 15 cout<<"endptr of li2 is: "<<&pEnd<<"its value is: "<<*pEnd<<endl; 16 li3 = strtol (pEnd,&pEnd,2); 17 cout<<"endptr of li3 is: "<<&pEnd<<"its value is: "<<*pEnd<<endl; 18 li4 = strtol (pEnd,NULL,0); 19 cout<<"endptr of li4 is: "<<&pEnd<<"its value is: "<<*pEnd<<endl; 20 printf ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n", li1, li2, li3, li4); 21 return 0; 22 }
输出结果:
1 endptr of li1 is: 0x8046c8cits value is: //间隔的空格 2 endptr of li2 is: 0x8046c8cits value is: 3 endptr of li3 is: 0x8046c8cits value is: 4 endptr of li4 is: 0x8046c8cits value is: 5 The decimal equivalents are: 2001, 6340800, -3624224 and 7340031.
*********************
strtod 由string转换为double类型的函数: double strtod ( const char * str, char ** endptr );
/* strtod example */ #include <stdio.h> #include <stdlib.h> int main () { char szOrbits[] = "365.24 29.53"; char * pEnd; double d1, d2; //pEnd是个指针,去指针的地址就是 指针的指针, 即 **pEnd d1 = strtod (szOrbits,&pEnd); //第二个参数的指针可以读去下一个double类型数据的地址,并作为下一个strtod的第一个参数传入,以取得string中第二部分的 double的值 d2 = strtod (pEnd,NULL); printf ("The moon completes %.2lf orbits per Earth year.\n", d1/d2); return 0; } Output: The moon completes 12.37 orbits per Earth year.