`
java-mans
  • 浏览: 11430372 次
文章分类
社区版块
存档分类
最新评论

strtol()函数详解

 
阅读更多

strtol()函数的原型为:

long int strtol(const char *nptr,char **endptr,int base);

函数的解释说明
  这个函数会将参数nptr字符串根据参数base来转换成长整型数。参数base范围从2至36,或0。参数base代表采的进制方式,如base值为10则采用10进制,若base值为16则采用16进制等。当base值为0时则是采用10进制做转换,但遇到如’0x’前置字符则会使用16进制做转换、遇到’0’前置字符而不是’0x’的时候会使用8进制做转换。一开始strtol()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,再遇到非数字或字符串结束时('\0')结束转换,并将结果返回。若参数endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr返回。

strtol是atoi的增强版
主要体现在这几方面:
  1、不仅可以识别十进制整数,还可以识别其它进制的整数,取决于base参数,比如strtol("0XDEADbeE~~", NULL, 16)返回0xdeadbee的值,strtol("0777~~", NULL, 8)返回0777的值。

2、endptr是一个传出参数,函数返回时指向后面未被识别的第一个字符。例如char *pos; strtol("123abc", &pos, 10);,strtol返回123,pos指向字符串中的字母a。如果字符串开头没有可识别的整数,例如char *pos; strtol("ABCabc", &pos, 10);,则strtol返回0,pos指向字符串开头,可以据此判断这种出错的情况,而这是atoi处理不了的。

3、如果字符串中的整数值超出long int的表示范围(上溢或下溢),则strtol返回它所能表示的最大(或最小)整数,并设置errno为ERANGE,例如strtol("0XDEADbeef~~", NULL, 16)返回0x7fffffff并设置errno为ERANGE

多数情况下,endptr设置为NULL, 即不返回非法字符串。


下面看几个例子:
------------------------------------------------------
char buffer[20]="10379cend$3";
char *stop;
printf("%d\n",strtol(buffer, &stop, 2));
printf("%s\n", stop);
输出结果:
2
379cend$3
-------------------------------------------------------
char buffer[20]="10379cend$3";
char *stop;
printf("%d\n",strtol(buffer, &stop, 8));
printf("%s\n", stop);
输出结果:
543
9cend$3
--------------------------------------------------------
char buffer[20]="10379cend$3";
char *stop;
printf("%d\n",strtol(buffer, &stop, 10));
printf("%s\n", stop);
输出结果:
10379
cend$3
-------------------------------------------------------
char buffer[20]="10379cend$3";
char *stop;
printf("%d\n",strtol(buffer, &stop, 16));
printf("%s\n", stop);
输出结果:
17005006
nd$3
另外,如果base为0,且字符串不是以0x(或者0X)开头,则按十进制进行转化。如果base为0或者16,并且字符串以0x(或者0X)开头,那么,x(或者X)被忽略,字符串按16进制转化。如果base不等于0和16,并且字符串以0x(或者0X)开头,那么x被视为非法字符。
例如:
-------------------------------------------------------
char buffer[20]="0x31da6c";
char *stop;
printf("%d\n",strtol(buffer, &stop, 0));
printf("%s\n", stop);
输出结果(stop为空):
3267180

-------------------------------------------------------
char buffer[20]="0x31da6c";
char *stop;
printf("%d\n",strtol(buffer, &stop, 13));
printf("%s\n", stop);
输出结果:
0
0x31da6c
-------------------------------------------------------

最后,需要说明的是,对于nptr指向的字符串,其开头和结尾处的空格被忽视,字符串中间的空格被视为非法字符。
例如:
-------------------------------------------------------
char buffer_1[20]="10379c";
char buffer_2[20]=" 10379c ";
char buffer_3[20]=" 10 379c ";
printf("%d\n",strtol(buffer_1,NULL,0));
printf("%d\n",strtol(buffer_2,NULL,0));
printf("%d\n",strtol(buffer_3,NULL,0));
输出结果为:
10379
10379
10

分享到:
评论

相关推荐

    strtol介绍

    详细介绍strtol函数的使用方法和案例介绍。 

    浅析C语言中strtol()函数与strtoul()函数的用法

    主要介绍了浅析C语言中strtol()函数与strtoul()函数的用法,注意其将字符串转换成long型的区别,需要的朋友可以参考下

    strtol是atoi的增强版

    atoi是一个常用的函数,用于将字符串转换成int。而strtol是atoi的增强版

    atoi,atol,strtod,strtol,strtoul实现类型转换.doc

    atoi,atol,strtod,strtol,strtoul实现类型转换.doc

    -C++参考大全(第四版) (2010 年度畅销榜

    30.25 strtol函数 30.26 strtoul函数 30.27 system函数 30.28 va_arg,va_start和va end函数. 30.29 wcstombs函数 30.30 wctomb函数 第31章 宽字符函数 31.1 宽字符分类函数 31.2 宽字符I/O函数 31.3 宽字符串函数 ...

    c语言字符串_数字转换函数大全

    strtol(将字符串转换成长整型数) strtoul(将字符串转换成无符号长整型数) toascii(将整型数转换成合法的ASCII 码字符) toupper(将小写字母转换成大写字母) tolower(将大写字母转换成小写字母) …… …… 各种转换...

    C编码技术-错误与对策

    strtol系列函数 数学函数的定义域与值域 字符串内容 字符串空间 内存分配 敏感信息 IO的格式字符串 FGETS FOPEN 跨系统传输二进制文件 文本模式和二进制模式 在信号处理函数中只调用异步安全的函数 程序终止时的清理...

    U-Boot 源码分析(u-boot-2009.03)

    U-Boot 源码分析(u-boot-2009.03),后续会有移植详细教程。

    C语言常用数字和字符串转换函数

    C语言常用数字和字符串转换函数,toi 字符串转换成整型数 atol 字符串转换成长整型数 atof 字符串转换成浮点型数 strtol 字符串转换成长整型数 strtoul 字符串转换成无符号长整型数 strtod 字符串转换成浮点数

    Keil C51 库函数源码

    声 明: 逆向以学习和研究为目的,版权属于Keil公司. ...strtol strtoul atan2 ceil cosh floor fmod modf pow sinh tanh gets strcat strcspn strncat strncmp strncpy strpbrk strrchr strrpbrk strrpos strspn strstr

    c语言标准库中字符转换函数和数字转换函数

    字符转换为数字: #include atoi();将字符转换为整型 例:char ch1;int i=atoi(ch1); atol();将字符转化为长整型 例:char ch2;long l=atol(ch2); ...将字符转化为浮点型 例:... long int li=strtol(str2); strtoul

    matlab-stoi示例.docx

    stoi函数matlab function template std::stoi int stoi (const string& str, size_t* idx = 0, int base = 10);int stoi (const wstring& str, size_t* idx = 0, int base = 10); Convert string to integer ...

    Linux C 函数参考.zip

    strtol(将字符串转换成长整型数) strtoul(将字符串转换成无符号长整型数) toascii(将整型数转换成合法的ASCII码字符) tolower(将大写字母转换成小写字母) toupper(将小写字母转换成大写字母) Calloc(配置...

    linux_c API函数大全

    strtol(将字符串转换成长整型数) 24 2.7 25 strtoul(将字符串转换成无符号长整型数) 25 2.8 25 toascii(将整型数转换成合法的ASCII 码字符) 25 2.9 26 tolower(将大写字母转换成小写字母) 26 2.10 26 ...

    optional-lite:optional lite - 一个类似于 C++17 的可选对象,一个用于 C++98、C++11 和更高版本的单文件头库中的可为空对象

    optional lite:C++17-like optional 的单文件头版本,C++98、C++11 和更高版本的可为空对象 内容std::optional、optional lite 和Boost.Optional 的比较报告与构建测试实施... const int value = strtol ( text, &pos,

    \Linux c函数参考

    17 strtol(将字符串转换成长整型数) ............................................................................ 17 strtoul(将字符串转换成无符号长整型数) ..............................................

    轻松实现C/C++各种常见进制相互转换

    long strtol( const char *str, char **str_end, int base); 参数详细说明请 参考文档 注意:这个函数在c标准库stdlib中,所以需要 #include 用法参考 #include #include #include int main(void) { // ...

    c/c++函数库说明(api)html版

    所有的 C / C++ 函数 Constructors (cppstring) Constructors (cppvector) Operators (cppbitset) Operators (cppdeque) Operators (cppstack) Operators (cppstring) Operators (cppvector) abort (stdother...

    C ++ 17:string_view转换为整数类型

    使用Boost Spirit Qi v2将string_view转换为整数类型

Global site tag (gtag.js) - Google Analytics