本文转载自:http://blog.csdn.net/cwqbuptcwqbupt/article/details/7518582

看了atol的实现,发现char到int的转换比较奇怪:c = (int)(unsigned char)*nptr++; 先将char转为unsigned再转为int,于是测试了下,发现有如下结果:
void main()
{
    char c = 0x80;
    unsigned uc = 0x80;
    printf("c2i=%x,c2ui=%x,uc2i=%x,uc2ui=%x\n", \
    (int)c,(unsigned int)c,(int)uc,(unsigned int)uc
    );
}
结果:
c2i=ffffff80,c2ui=ffffff80,uc2i=80,uc2ui=80

可以发现,如果char默认为signed(可能是平台相关的),则将char转为int或uint时,会有符号位扩展,而unsigned char则不会。atol/atoi函数应该希望避免符号位扩展而带来问题。不过,好在数字0到9的ACSII码并没有超过0x7F,因此是否事先转成unsigned char应该不会对结果有影响。

另,转一篇类似问题造成的BUG:http://testing.etao.com/node/217

另外,atoi/atol是笔试面试常考问题,虽然看似不难,但往往实现起来漏洞百出。实现时注意以下几点:
1. 跳过开头空格。
2. 判断第一个有效字符(非空格)是否是符号‘+’或‘-’。
3. 当遇到非数字时,函数结束,输出之前字串代表的整数。
4. 为严谨起见,就是文中提到的的char转int问题。

附atol源码:

    1. long __cdecl atol(
    2. const char *nptr
    3. )
    4. {
    5. int c;              /* current char */
    6. long total;         /* current total */
    7. int sign;           /* if '-', then negative, otherwise positive */
    8. /* skip whitespace */
    9. while ( isspace((int)(unsigned char)*nptr) )
    10. ++nptr;
    11. c = (int)(unsigned char)*nptr++;
    12. sign = c;           /* save sign indication */
    13. if (c == '-' || c == '+')
    14. c = (int)(unsigned char)*nptr++;    /* skip sign */
    15. total = 0;
    16. while (isdigit(c)) {
    17. total = 10 * total + (c - '0');     /* accumulate digit */
    18. c = (int)(unsigned char)*nptr++;    /* get next char */
    19. }
    20. if (sign == '-')
    21. return -total;
    22. else
    23. return total;   /* return result, negated if necessary */
    24. }

atol的实现【转】的更多相关文章

  1. Linux下c++中的atoi、atol、atoll、atof函数调用实例

    本文中调用的四个函数如下: atoi函数:将字符串转化为int类型变量 atol函数:将字符串转化为long类型变量 atoll函数:将字符串转化为long long类型变量 atof函数:将字符串转 ...

  2. C++ atol

    函数名: atol 功 能: 把字符串转换成长整型数 用 法: long atol(const char *nptr);   简介编辑 相关函数: atof,atoi,strtod,strtol,st ...

  3. atol字符串转换函数应用实例

    原型:long atol(const char *nptr); 相关函数 atoi,atol,strtod,strtol,strtoul 头文件:stdlib.h 功能:将字符串转换成整型数 说明:参 ...

  4. atoi atol strtod strtol strtoul _gcvt

    如果以下函数,您在使用的时候,总是输出一个莫名的值,是因为您忘记了引用头文件 #include <stdlib.h> 1- atoi int atoi(const char *nptr); ...

  5. atof()函数 atol()

    atof()函数 atof():double atof(const char *str ); 功 能: 把字符串转换成浮点数 str:要转换的字符串. 返回值:每个函数返回 double 值,此值由将 ...

  6. [trouble shoot]atol和atoll

    就终于的结果来看,事实上就是一个小的错误. 但定位错误的时间比較漫长了.. . 背景:出错的代码是 一段执行在 linux server上的程序,程序的主要功能是处理银行pos刷卡记录并做一些计算.最 ...

  7. minix中atoi、atol、atof的实现

    在minix2.0源代码中,有将字符串类型转换为int.long.double类型的函数实现,相关的实现函数分别在atoi.c.atol.c.atof.c文件中,我们来逐一学习其中的源码: 1.int ...

  8. 字符串转换atof atoi atol gcvt strtod strtol strto ul toascii tolower toupper

    atof(将字符串转换成浮点型数) 相关函数 atoi,atol,strtod,strtol,strtoul 表头文件 #include <stdlib.h> 定义函数 double at ...

  9. 函数atof,atoi,atol,strtod,strtol,strtoul 描述

    函数atof,atoi,atol,strtod,strtol,strtoul atof(将字串转换成浮点型数) 相关函数 atoi,atol,strtod,strtol,strtoul表头文件 #in ...

  10. strtol函數的用法 atof, atoi, atol, strtod, strtoul

    相关函数: atof, atoi, atol, strtod, strtoul表头文件: #include <stdlib.h>定义函数: long int strtol(const ch ...

随机推荐

  1. Java 基础【03】序列化和反序列化

    当两个进程在进行远程通信时,彼此可以发送各种类型的数据.无论是何种类型的数据,都会以二进制序列的形式在网络上传送.发送方需要把这个Java对象转换为字节序列,才能在网络上传送:接收方则需要把字节序列再 ...

  2. Wireshark如何选择多行

    Wireshark如何选择多行   在Wireshark中,用户经常需要选择几行,然后进行批量操作,如导出或者分析.但Wireshark没有提供通过鼠标直接选择多行的功能.这个时候,用户需要采用标记分 ...

  3. Springboot 集成 Thymeleaf 及常见错误

    Thymeleaf模板引擎是springboot中默认配置,与freemarker相似,可以完全取代jsp,在springboot中,它的默认路径是src/main/resources/templat ...

  4. jQuery使用on()绑定动态生成元素的事件无效

    jquery on()方法是jquery1.7+后才使用的 由于需求:动态添加了以下代码 <tr class="pj" data-val="no"> ...

  5. java后4位打成*显示

    /** * [固定电话] 后四位,其他隐藏<例子:****1234> * * @param num * @return */ public static String fixedPhone ...

  6. android 根据图片名字获取图片id

    public int getResource(String imageName){ Context ctx=getBaseContext(); int resId = getResources().g ...

  7. 【前台 ajax】前台ajax请求,地址正确,但是报错不进入后台

    前台ajax请求,地址正确,但是报错不进入后台 出现上述问题,可能的情况是 1.ajax用的post,而后台限定用get,或者所有的post请求都被拦截,所以不能正常进入并且报错403 @Reques ...

  8. Activity的启动模式全解standard,singleTop,singleTask,singleInstance

    在android中控制Activity的启动模式的属性主要控制两大功能: 1,控制activity 进入哪一个任务task 中,   有两种可能,进入启动task中,进入指定taskAffinity的 ...

  9. iOS开发 编码规范

    转至   http://www.cnblogs.com/celestial/archive/2012/06/30/2571417.html 编码规范 一.文档结构管理 1.建立Libraries文件夹 ...

  10. js:深入继承

    /**  * js实现继承:  * 1.基于原型链的方式  * 2.基于伪造的方式  * 3.基于组合的方式  */ 一.基于原型链的方式 function Parent(){   this.pv = ...