- /***
- *atox.c-atoiandatolconversion
- *
- *Copyright(c)1989-1997,MicrosoftCorporation.Allrightsreserved.
- *
- *Purpose:
- *Convertsacharacterstringintoanintorlong.
- *
- *******************************************************************************/
- #include<cruntime.h>
- #include<stdlib.h>
- #include<ctype.h>
- /***
- *longatol(char*nptr)-Convertstringtolong
- *
- *Purpose:
- *ConvertsASCIIstringpointedtobynptrtobinary.
- *Overflowisnotdetected.
- *
- *Entry:
- *nptr=ptrtostringtoconvert
- *
- *Exit:
- *returnlongintvalueofthestring
- *
- *Exceptions:
- *None-overflowisnotdetected.
- *
- *******************************************************************************/
- long__cdeclatol(
- constchar*nptr
- )
- {
- intc;/*currentchar*/
- longtotal;/*currenttotal*/
- intsign;/*if''-'',thennegative,otherwisepositive*/
- /*skipwhitespace*/
- while(isspace((int)(unsignedchar)*nptr))
- ++nptr;
- c=(int)(unsignedchar)*nptr++;
- sign=c;/*savesignindication*/
- if(c==''-''||c==''+'')
- c=(int)(unsignedchar)*nptr++;/*skipsign*/
- total=0;
- while(isdigit(c)){
- total=10*total+(c-''0'');/*accumulatedigit*/
- c=(int)(unsignedchar)*nptr++;/*getnextchar*/
- }
- if(sign==''-'')
- return-total;
- else
- returntotal;/*returnresult,negatedifnecessary*/
- }
- /***
- *intatoi(char*nptr)-Convertstringtolong
- *
- *Purpose:
- *ConvertsASCIIstringpointedtobynptrtobinary.
- *Overflowisnotdetected.Becauseofthis,wecanjustuse
- *atol().
- *
- *Entry:
- *nptr=ptrtostringtoconvert
- *
- *Exit:
- *returnintvalueofthestring
- *
- *Exceptions:
- *None-overflowisnotdetected.
- *
- *******************************************************************************/
- int__cdeclatoi(
- constchar*nptr
- )
- {
- return(int)atol(nptr);
- }
- #ifndef_NO_INT64
- __int64__cdecl_atoi64(
- constchar*nptr
- )
- {
- intc;/*currentchar*/
- __int64total;/*currenttotal*/
- intsign;/*if''-'',thennegative,otherwisepositive*/
- /*skipwhitespace*/
- while(isspace((int)(unsignedchar)*nptr))
- ++nptr;
- c=(int)(unsignedchar)*nptr++;
- sign=c;/*savesignindication*/
- if(c==''-''||c==''+'')
- c=(int)(unsignedchar)*nptr++;/*skipsign*/
- total=0;
- while(isdigit(c)){
- total=10*total+(c-''0'');/*accumulatedigit*/
- c=(int)(unsignedchar)*nptr++;/*getnextchar*/
- }
- if(sign==''-'')
- return-total;
- else
- returntotal;/*returnresult,negatedifnecessary*/
- }
- #endif/*_NO_INT64*/
- #include<msvcrt/errno.h>
- #include<msvcrt/stdlib.h>
- #include<msvcrt/internal/file.h>
- char*_itoa(intvalue,char*string,intradix)
- {
- chartmp[33];
- char*tp=tmp;
- inti;
- unsignedv;
- intsign;
- char*sp;
- if(radix>36||radix<=1)
- {
- __set_errno(EDOM);
- return0;
- }
- 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;
- returnstring;
- }
- PS1:atoi()可以用二维数组的索引来实现,避免*10的循环。
- PS2:itoa()采用返回堆指针的方式得到结果,不会造成memoryleak和指针问题。但是不可以通过传入的char*string返回结果,会造成指针问题。(可以改写为char**string,因此微软的sdk中实现的方式有问题)
- 注:源码可在VC目录下找到