atol的实现【转】
本文转载自: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源码:
- long __cdecl atol(
- const char *nptr
- )
- {
- int c; /* current char */
- long total; /* current total */
- int sign; /* if '-', then negative, otherwise positive */
- /* skip whitespace */
- while ( isspace((int)(unsigned char)*nptr) )
- ++nptr;
- c = (int)(unsigned char)*nptr++;
- sign = c; /* save sign indication */
- if (c == '-' || c == '+')
- c = (int)(unsigned char)*nptr++; /* skip sign */
- total = 0;
- while (isdigit(c)) {
- total = 10 * total + (c - '0'); /* accumulate digit */
- c = (int)(unsigned char)*nptr++; /* get next char */
- }
- if (sign == '-')
- return -total;
- else
- return total; /* return result, negated if necessary */
- }
atol的实现【转】的更多相关文章
- Linux下c++中的atoi、atol、atoll、atof函数调用实例
本文中调用的四个函数如下: atoi函数:将字符串转化为int类型变量 atol函数:将字符串转化为long类型变量 atoll函数:将字符串转化为long long类型变量 atof函数:将字符串转 ...
- C++ atol
函数名: atol 功 能: 把字符串转换成长整型数 用 法: long atol(const char *nptr); 简介编辑 相关函数: atof,atoi,strtod,strtol,st ...
- atol字符串转换函数应用实例
原型:long atol(const char *nptr); 相关函数 atoi,atol,strtod,strtol,strtoul 头文件:stdlib.h 功能:将字符串转换成整型数 说明:参 ...
- atoi atol strtod strtol strtoul _gcvt
如果以下函数,您在使用的时候,总是输出一个莫名的值,是因为您忘记了引用头文件 #include <stdlib.h> 1- atoi int atoi(const char *nptr); ...
- atof()函数 atol()
atof()函数 atof():double atof(const char *str ); 功 能: 把字符串转换成浮点数 str:要转换的字符串. 返回值:每个函数返回 double 值,此值由将 ...
- [trouble shoot]atol和atoll
就终于的结果来看,事实上就是一个小的错误. 但定位错误的时间比較漫长了.. . 背景:出错的代码是 一段执行在 linux server上的程序,程序的主要功能是处理银行pos刷卡记录并做一些计算.最 ...
- minix中atoi、atol、atof的实现
在minix2.0源代码中,有将字符串类型转换为int.long.double类型的函数实现,相关的实现函数分别在atoi.c.atol.c.atof.c文件中,我们来逐一学习其中的源码: 1.int ...
- 字符串转换atof atoi atol gcvt strtod strtol strto ul toascii tolower toupper
atof(将字符串转换成浮点型数) 相关函数 atoi,atol,strtod,strtol,strtoul 表头文件 #include <stdlib.h> 定义函数 double at ...
- 函数atof,atoi,atol,strtod,strtol,strtoul 描述
函数atof,atoi,atol,strtod,strtol,strtoul atof(将字串转换成浮点型数) 相关函数 atoi,atol,strtod,strtol,strtoul表头文件 #in ...
- strtol函數的用法 atof, atoi, atol, strtod, strtoul
相关函数: atof, atoi, atol, strtod, strtoul表头文件: #include <stdlib.h>定义函数: long int strtol(const ch ...
随机推荐
- Xcode文件名后的字母含义
Xcode文件名后的字母含义 在Xcode中,左侧的dock区域显示项目结构.很多时候,文件名后方会出现一个字母提示,如M.A之类的.这实际是一种提示符号.如果项目中使用SVN.Git等版本控制工 ...
- 2002-2003 ACM-ICPC Northeastern European Regional Contest (NEERC 02)
B Bricks 计算几何乱搞 题意: 给你个立方体,问你能不能放进一个管道里面. 题解: 这是一道非常迷的题,其问题在于,你可以不正着放下去,你需要斜着放.此时你需要枚举你旋转的角度,来判断是否可行 ...
- WebLogic 11g重置用户密码
weblogic安装后,很久不用,忘记访问控制台的用户名或者密码,可通过以下步骤来重置用户名密码. 版本:WebLogic Server 11g 说明:%DOMAIN_HOME%:指WebLogic ...
- 5.【nuxt起步】-swiper组件
接下来是一个比较常用,也比较重要的组件 swiper,可以自行搜索 vue swiper,有很多开源组件,我这里就复用之前一个熟悉的, 1.新建component/banner.vue 刷新报错: 要 ...
- 辛星浅析raid
我们来分析一下影响计算机性能的主要组件,主要就是CPU.主板的总线IO.内存IO.硬盘IO.网卡IO等等,并且如今CPU的性能已经非常好了,可是计算机的总体的IO性能却较低,严重影响了计算机的性能,如 ...
- WMS8_条码界面操作简要说明(包装作业)
说明:条码界面的主要用途是包装作业 这个客户端,完全是JS实现的 可以从 All operation看板视图 Picking的表单视图 All operatio ...
- Path SumI、II——给出一个数,从根到子的和等于它
I.Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up a ...
- Android——动画的分类
Android包含三种动画:View Animation, Drawable Animation, Property Animation(Android 3.0新引入). 1.View Animati ...
- cocos2d-x入口类
上一篇文章中有一个在栈中创建的实例--AppDelegate.这个类的初始化使cocos2d-x的程序能够执行起来.由于它是继承于CCApplication类.而执行的run方法就是在此类中实现的. ...
- MOS管驱动详解
1.常用的几种电平转换方案 2.三极管的电平转换及驱动电路分析 3.三级管老怀 4.关于MOSFET管驱动电路总结 5.一个IIC的5V和3.3V电平转换的经典电路分享 6.mos 7.mos应用 8 ...