一)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_t
ngx_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;
else
return 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;
else
return 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;
else
return 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;
else
v = (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();// 将无符号长整型值转换为字符串