`
hulianwang2014
  • 浏览: 691358 次
文章分类
社区版块
存档分类
最新评论
  • bcworld: 排版成这样,一点看的欲望都没有了
    jfinal

itoa()和atoi()/atol()的源码

 
阅读更多
  1. /***
  2. *atox.c-atoiandatolconversion
  3. *
  4. *Copyright(c)1989-1997,MicrosoftCorporation.Allrightsreserved.
  5. *
  6. *Purpose:
  7. *Convertsacharacterstringintoanintorlong.
  8. *
  9. *******************************************************************************/
  10. #include<cruntime.h>
  11. #include<stdlib.h>
  12. #include<ctype.h>
  13. /***
  14. *longatol(char*nptr)-Convertstringtolong
  15. *
  16. *Purpose:
  17. *ConvertsASCIIstringpointedtobynptrtobinary.
  18. *Overflowisnotdetected.
  19. *
  20. *Entry:
  21. *nptr=ptrtostringtoconvert
  22. *
  23. *Exit:
  24. *returnlongintvalueofthestring
  25. *
  26. *Exceptions:
  27. *None-overflowisnotdetected.
  28. *
  29. *******************************************************************************/
  30. long__cdeclatol(
  31. constchar*nptr
  32. )
  33. {
  34. intc;/*currentchar*/
  35. longtotal;/*currenttotal*/
  36. intsign;/*if''-'',thennegative,otherwisepositive*/
  37. /*skipwhitespace*/
  38. while(isspace((int)(unsignedchar)*nptr))
  39. ++nptr;
  40. c=(int)(unsignedchar)*nptr++;
  41. sign=c;/*savesignindication*/
  42. if(c==''-''||c==''+'')
  43. c=(int)(unsignedchar)*nptr++;/*skipsign*/
  44. total=0;
  45. while(isdigit(c)){
  46. total=10*total+(c-''0'');/*accumulatedigit*/
  47. c=(int)(unsignedchar)*nptr++;/*getnextchar*/
  48. }
  49. if(sign==''-'')
  50. return-total;
  51. else
  52. returntotal;/*returnresult,negatedifnecessary*/
  53. }
  54. /***
  55. *intatoi(char*nptr)-Convertstringtolong
  56. *
  57. *Purpose:
  58. *ConvertsASCIIstringpointedtobynptrtobinary.
  59. *Overflowisnotdetected.Becauseofthis,wecanjustuse
  60. *atol().
  61. *
  62. *Entry:
  63. *nptr=ptrtostringtoconvert
  64. *
  65. *Exit:
  66. *returnintvalueofthestring
  67. *
  68. *Exceptions:
  69. *None-overflowisnotdetected.
  70. *
  71. *******************************************************************************/
  72. int__cdeclatoi(
  73. constchar*nptr
  74. )
  75. {
  76. return(int)atol(nptr);
  77. }
  78. #ifndef_NO_INT64
  79. __int64__cdecl_atoi64(
  80. constchar*nptr
  81. )
  82. {
  83. intc;/*currentchar*/
  84. __int64total;/*currenttotal*/
  85. intsign;/*if''-'',thennegative,otherwisepositive*/
  86. /*skipwhitespace*/
  87. while(isspace((int)(unsignedchar)*nptr))
  88. ++nptr;
  89. c=(int)(unsignedchar)*nptr++;
  90. sign=c;/*savesignindication*/
  91. if(c==''-''||c==''+'')
  92. c=(int)(unsignedchar)*nptr++;/*skipsign*/
  93. total=0;
  94. while(isdigit(c)){
  95. total=10*total+(c-''0'');/*accumulatedigit*/
  96. c=(int)(unsignedchar)*nptr++;/*getnextchar*/
  97. }
  98. if(sign==''-'')
  99. return-total;
  100. else
  101. returntotal;/*returnresult,negatedifnecessary*/
  102. }
  103. #endif/*_NO_INT64*/
  104. #include<msvcrt/errno.h>
  105. #include<msvcrt/stdlib.h>
  106. #include<msvcrt/internal/file.h>
  107. char*_itoa(intvalue,char*string,intradix)
  108. {
  109. chartmp[33];
  110. char*tp=tmp;
  111. inti;
  112. unsignedv;
  113. intsign;
  114. char*sp;
  115. if(radix>36||radix<=1)
  116. {
  117. __set_errno(EDOM);
  118. return0;
  119. }
  120. sign=(radix==10&&value<0);
  121. if(sign)
  122. v=-value;
  123. else
  124. v=(unsigned)value;
  125. while(v||tp==tmp)
  126. {
  127. i=v%radix;
  128. v=v/radix;
  129. if(i<10)
  130. *tp++=i+''0'';
  131. else
  132. *tp++=i+''a''-10;
  133. }
  134. if(string==0)
  135. string=(char*)malloc((tp-tmp)+sign+1);
  136. sp=string;
  137. if(sign)
  138. *sp++=''-'';
  139. while(tp>tmp)
  140. *sp++=*--tp;
  141. *sp=0;
  142. returnstring;
  143. }
  144. PS1:atoi()可以用二维数组的索引来实现,避免*10的循环。
  145. PS2:itoa()采用返回堆指针的方式得到结果,不会造成memoryleak和指针问题。但是不可以通过传入的char*string返回结果,会造成指针问题。(可以改写为char**string,因此微软的sdk中实现的方式有问题)
  146. 注:源码可在VC目录下找到
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics