# atoi和itoa函数及负数转正数时溢出问题

isspace函数是判断传入字符是否为空白符, 空白符指空格, 水平制表, 垂直制表, 换页, 回车和换行符.

atoi函数:

/***
*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++;
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
}

/***
*int atoi(char *nptr) - Convert string to long
*
*Purpose:
*       Converts ASCII string pointed to by nptr to binary.
*       Overflow is not detected.  Because of this, we can just use
*       atol().
*
*Entry:
*       nptr = ptr to string to convert
*
*Exit:
*       return int value of the string
*
*Exceptions:
*       None - overflow is not detected.
*
*******************************************************************************/

int __cdecl atoi(
const char *nptr
)
{
return (int)atol(nptr);
}

char * my_itoa( int num, char*str, int radix )
{
const char table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
char*ptr=str ;
bool negative=false ;
if(num==0)
{
//num=0
*ptr++='0' ;
*ptr='/0' ;
// dont forget the end of the string is '/0'!!!!!!!!!
return str ;
}
if(num<0)
{
//if num is negative ,the add '-'and change num to positive
*ptr++='-' ;
num*=-1 ;
negative=true ;
}
while(num)
{
}
*ptr='/0' ;
//if num is negative ,the add '-'and change num to positive
// in the below, we have to converse the string
char*start=(negative?str+1:str);
//now start points the head of the string
ptr--;
//now prt points the end of the string
while(start<ptr)
{
char temp=*start ;
*start=*ptr ;
*ptr=temp ;
start++;
ptr--;
}
return str ;
}

static void __cdecl xtoa (
unsigned long val,
char *buf,
int is_neg
)
{
char *p;                /* pointer to traverse string */
char *firstdig;         /* pointer to first digit */
char temp;              /* temp char */
unsigned digval;        /* value of digit */

p = buf;

if (is_neg) {
/* negative, so output '-' and negate */
*p++ = '-';
val = (unsigned long)(-(long)val);
}

firstdig = p;           /* save pointer to first digit */

do {
digval = (unsigned) (val % radix);
val /= radix;       /* get next digit */

/* convert to ascii and store */
if (digval > 9)
*p++ = (char) (digval - 10 + 'a');  /* a letter */
else
*p++ = (char) (digval + '0');       /* a digit */
} while (val > 0);

/* We now have the digit of the number in the buffer, but in reverse
order.  Thus we reverse them now. */

*p-- = '\0';            /* terminate string; p points to last digit */

do {
temp = *p;
*p = *firstdig;
*firstdig = temp;   /* swap *p and *firstdig */
--p;
++firstdig;         /* advance to next two digits */
} while (firstdig < p); /* repeat until halfway */
}

/* Actual functions just call conversion helper with neg flag set correctly,
and return pointer to buffer. */

char * __cdecl _itoa (
int val,
char *buf,
)
{
if (radix == 10 && val < 0)
else
xtoa((unsigned long)(unsigned int)val, buf, radix, 0);
return buf;
}

char * itoa_modified( int    val,
char  *buf,
{
char *p, *firstdig;
char temp;              /* temp char */

p = buf;
if ( val < 0 ) {
*p++ = '-';
}
if ( val > 0 ) {
val = -val;
}
firstdig = p;
do {
*p++ = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[-( val % radix )];
} while ( val );
*p-- = '\0';
do {
temp = *p;
*p = *firstdig;
*firstdig = temp; /* swap *p and *firstdig */
--p;
++firstdig; /* advance to next two digits */
} while ( firstdig < p ); /* repeat until halfway */
return ( buf );
}`

• 0
点赞
• 0
评论
• 1
收藏
• 打赏
• 扫一扫，分享海报

04-10 504

10-05 1129
06-26 870
03-29 1799
08-14 6771
03-20 623
07-15 191
11-16 1864
06-08 6122

dq_zhx

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。