atoi 与 itoa的详解和实现源代码
来源:互联网 发布:indesign可变数据 编辑:程序博客网 时间:2024/05/29 14:36
一)C语言库函数名: int atoi(const char *nptr);
①函数解释
功 能: 把字符串转换成整型数.
名字来源:array to integer 的缩写.
函数说明: 参数nptr字符串,如果第一个非空格字符不存在或者不是数字也不是正负号则返回零,否则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时停止转换,返回整型数。
头文件: #include <stdlib.h>
②使用例子:
#include <stdio.h>#include <stdlib.h>int main(void){int n;char *str = "12345.67";n = atoi(str);printf("string = %s integer = %d\n", str, n);char a[] = "-100" ;char b[] = "123" ;int c = atoi( a ) + atoi( b ) ;printf("c = %d\n", c) ;return 0;}//执行结果//string = 12345.67 integer = 12345//c = 23
③实现atoi()和atol ()的源代码(32位机器):
Nginx实现
ngx_int_tngx_atoi(u_char *line, size_t n){ ngx_int_t value; if (n == 0) { return NGX_ERROR; } for (value = 0; n--; line++) { if (*line < '0' || *line > '9') { return NGX_ERROR; } value = value * 10 + (*line - '0'); } if (value < 0) { return NGX_ERROR; } else { return value; } }
#include <cruntime.h>#include <stdlib.h>#include <ctype.h>/****long atol(char *nptr) - Convert string to long**Purpose:* Converts ASCII string pointed to by nptr to binary.* Overflow is not detected.**Entry:* nptr = ptr to string to convert**Exit:* return long int value of the string**Exceptions:* None - overflow is not detected.********************************************************************************/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;elsereturn total; /* return result, negated if necessary */}
int __cdecl atoi(const char *nptr){return (int)atol(nptr);}
说明:atol()只转换ASCII的数字,所以每次取下一字符时指针移动8位,传入的指针用const char有一部分原因是习惯问题;转换为unsigned char是因为ASCII字符是从0x0~0xff而没有负的,转换为int防止编译器的不同,自动转换的结果不同;、
自己实现的isdigit()函数:
inline int isdigit(unsigned char ch){const int m_nData = (int)(unsigned char)(ch - '0');if((m_nData >= 0) && (m_nData <=9))return m_nData;elsereturn 0;}
//溢出的现象:
// overflow if(total > std::numeric_limits<int>::max()) { total = 0; break; }
Microsoft:
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; else return 0; }
在64位机器上的实现:
#ifndef _NO_INT64__int64 __cdecl _atoi64(const char *nptr){int c;__int64 total;int sign;while ( isspace((int)(unsigned char)*nptr) )++nptr;c = (int)(unsigned char)*nptr++;sign = c;if (c == ''-'' || c == ''+'')c = (int)(unsigned char)*nptr++;total = 0;while (isdigit(c)) {total = 10 * total + (c - ''0'');c = (int)(unsigned char)*nptr++;}if (sign == ''-'')return -total;elsereturn total;}#endif
①函数解释:
参数说明:int value 被转换的整数,char *string 转换后储存的字符数组,int radix 转换进制数,如2,8,10,16 进制等。
返回值:指向string这个字符串的指针
②使用例子:
#include <stdlib.h>#include <stdio.h>int main(void){int number = 12345;char string[25];itoa(number, string, 10); //按十进制转换printf("integer = %d string = %s\n", number, string);itoa(number, string, 16); //按16进制转换printf("integer = %d string = %s\n", number, string);return 0;}//输出结果://integer = 12345 string = 12345 --说明12345的十进制表示就是12345//integer = 12345 string = 3039 ——说明12345的十六进制表示是0x3039
③实现itoa和ltoa ()的源代码:
char* itoa(int value, char* string, int radix){char tmp[33];char* tp = tmp;int i;unsigned v;int sign;char* sp;if (radix > 36 || radix <= 1){//__set_errno(EDOM);return 0;}sign = (radix == 10 && value < 0);if (sign)v = -value;elsev = (unsigned)value;while (v || tp == tmp){i = v % radix;v = v / radix;if (i < 10)*tp++ = i + '0';else*tp++ = i + 'a' - 10;}if (string == 0)string = (char*)malloc((tp-tmp)+sign+1);sp = string;if (sign)*sp++ = '-';while (tp > tmp)*sp++ = *--tp;*sp = '\0';return string;}
补充的部分:
itoa并不是一个标准的C函数,它是Windows特有的,如果要写跨平台的程序,请用sprintf.
char str[255];sprintf(str, "%x", 100); //将100转为16进制表示的字符串另外常见的相关函数有:
atol();//实现从字符串转换到长整型ltoa(); //将长整型值转换为字符串ultoa();// 将无符号长整型值转换为字符串
- atoi 与 itoa的详解和实现源代码
- 微软strcpy,strcat和strcmp的实现源代码 itoa atoi
- itoa与atoi源代码
- atoi 与 itoa的实现
- itoa与atoi的实现
- atoi 与 itoa的实现
- atoi和itoa的实现
- atoi和itoa的实现
- atoi 和itoa的实现
- atoi和itoa的实现
- atoi 和 itoa的实现
- atoi和itoa的实现
- atoi 和itoa实现
- atoi 和 itoa实现
- 实现itoa()和atoi()
- atoi() 与 itoa()函数的内部实现
- atoi() 与 itoa()函数的内部实现
- atoi() 与 itoa()函数的内部实现
- 毫西弗-辐射相关
- 关于字符串转化为整型数和浮点数
- hibernate中的component技术例子
- MALLET中的序列标注
- 赛门铁克企业级防病毒产品 》(Symantec Endpoint Protection)V11.0.6005.562 简体中文版[压缩包]
- atoi 与 itoa的详解和实现源代码
- openssl 代码分析(6) SSL_connect
- 信息摘要算法-RipeMD以及HmacRipeMD算法
- ext.tree动态加载
- 聚类分析中几种算法的比较
- .net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表...
- Ext.tree如何创建树以及动态加载树
- 跟我一起走进WPF的世界之一学了WPF能做什么?
- Servlet生命周期