atoi() 函数与 itoa() 函数:字符串与数值之间的转换
来源:互联网 发布:go并发编程实战 2017 编辑:程序博客网 时间:2024/05/22 08:11
在 C 语言中, atoi() 函数是实现字符串转换成数字,atoi() 函数源码:
isspace(int x){ if(x==' '||x=='\t'||x=='\n'||x=='\f'||x=='\b'||x=='\r') return 1; else return 0;}isdigit(int x){ if(x<='9'&&x>='0') return 1;x` else return 0;}int atoi(const char *nptr){ int c; /* current char */ int 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 */}
itoa() 函数是实现数字转换成字符串,源码如下:
void my_itoa(int n,char s[]) { int i,j,sign; if((sign=n)<0) //记录符号 n=-n; //使n成为正数 i=0; do{ s[i++]=n%10+'0'; //取下一个数字 }while((n/=10)>0); //循环相除 if(sign<0) s[i++]='-'; s[i]='\0'; for(j=i-1;j>=0;j--) //生成的数字是逆序的,所以要逆序输出 printf("%c",s[j]); }
阅读全文
2 0
- atoi() 函数与 itoa() 函数:字符串与数值之间的转换
- C语言数字与字符串转换 atoi()函数、itoa()函数、sprintf()函数
- atoi() 与 itoa()函数的内部实现
- atoi() 与 itoa()函数的内部实现
- atoi() 与 itoa()函数的内部实现
- atoi() 与 itoa()函数的内部实现
- atoi() 与 itoa()函数的用法
- atoi() 与 itoa()函数的内部实现
- atoi() 与 itoa()函数的内部实现
- atoi() 与 itoa()函数用法
- atoi() 与 itoa()函数用法
- atoi() 与 itoa()函数用法
- atoi, itoa 字符串转换函数源代码
- 编写整数字符串转化函数(不用itoa与atoi)
- atoi()函数原型与itoa()函数原型
- 数字字符串与数字相互转化-atoi函数和itoa函数的实现
- 字符串与数值相互转换的函数
- 字符串与数值相互转换的函数
- SQL Server 常用代码块
- 收藏的一些不错的多线程编程的文章
- 模块化
- Docker(镜像层缓存,Dockerfile调试)
- 调整数组顺序使奇数位于偶数之前
- atoi() 函数与 itoa() 函数:字符串与数值之间的转换
- linux学习第三十八篇: Apache和PHP结合,Apache默认虚拟主机
- Maven 简介
- Maven学习总结系列八:使用Nexus创建私服
- 一切从此开始
- Spring是什么
- 熵扰动和绝热扰动
- cxf框架的简介
- Lua——3.元表Metatable