atoi()和itoa()的标准源码实现

来源:互联网 发布:同视机如何看数据 编辑:程序博客网 时间:2024/06/04 23:27

atoi()和itoa()的标准源码实现


microsoft's version

[cpp] view plaincopy
  1. char* _itoa(int value, char* string, int radix)  
  2. {  
  3.     char tmp[33];  
  4.     char* tp = tmp;  
  5.     int i;  
  6.     unsigned v;  
  7.     int sign;  
  8.     char* sp;  
  9.     if (radix > 36 || radix <= 1)  
  10.     {  
  11.         __set_errno(EDOM);  
  12.         return 0;  
  13.     }  
  14.     sign = (radix == 10 && value < 0);  
  15.     if (sign)  
  16.         v = -value;  
  17.     else  
  18.         v = (unsigned)value;  
  19.     while (v || tp == tmp)  
  20.     {  
  21.         i = v % radix;  
  22.         v = v / radix;  
  23.         if (i < 10)  
  24.             *tp++ = i+''0'';  
  25.         else  
  26.             *tp++ = i + ''a'' - 10;  
  27.     }  
  28.     if (string == 0)  
  29.         string = (char*)malloc((tp-tmp)+sign+1);  
  30.     sp = string;  
  31.     if (sign)  
  32.         *sp++ = ''-'';  
  33.     while (tp > tmp)  
  34.         *sp++ = *--tp;  
  35.     *sp = 0;  
  36.     return string;  
  37. }  


atoi()

[cpp] view plaincopy
  1. /*** 
  2. *atox.c - atoi and atol conversion 
  3. * 
  4. * Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved. 
  5. * 
  6. *Purpose: 
  7. * Converts a character string into an int or long. 
  8. * 
  9. *******************************************************************************/  
  10.   
  11. #include <cruntime.h>  
  12. #include <stdlib.h>  
  13. #include <ctype.h>  
  14.   
  15. /*** 
  16. *long atol(char *nptr) - Convert string to long 
  17. * 
  18. *Purpose: 
  19. * Converts ASCII string pointed to by nptr to binary. 
  20. * Overflow is not detected. 
  21. * 
  22. *Entry: 
  23. * nptr = ptr to string to convert 
  24. * 
  25. *Exit: 
  26. * return long int value of the string 
  27. * 
  28. *Exceptions: 
  29. * None - overflow is not detected. 
  30. * 
  31. *******************************************************************************/  
  32.   
  33. long __cdecl atol(  
  34. const char *nptr  
  35. )  
  36. {  
  37. int c; /* current char */  
  38. long total; /* current total */  
  39. int sign; /* if ''-'', then negative, otherwise positive */  
  40.   
  41. /* skip whitespace */  
  42. while ( isspace((int)(unsigned char)*nptr) )  
  43. ++nptr;  
  44.   
  45. c = (int)(unsigned char)*nptr++;  
  46. sign = c; /* save sign indication */  
  47. if (c == ''-'' || c == ''+'')  
  48. c = (int)(unsigned char)*nptr++; /* skip sign */  
  49.   
  50. total = 0;  
  51.   
  52. while (isdigit(c)) {  
  53. total = 10 * total + (c - ''0''); /* accumulate digit */  
  54. c = (int)(unsigned char)*nptr++; /* get next char */  
  55. }  
  56.   
  57. if (sign == ''-'')  
  58. return -total;  
  59. else  
  60. return total; /* return result, negated if necessary */  
  61. }  
  62.   
  63.   
  64. /*** 
  65. *int atoi(char *nptr) - Convert string to long 
  66. * 
  67. *Purpose: 
  68. * Converts ASCII string pointed to by nptr to binary. 
  69. * Overflow is not detected. Because of this, we can just use 
  70. * atol(). 
  71. * 
  72. *Entry: 
  73. * nptr = ptr to string to convert 
  74. * 
  75. *Exit: 
  76. * return int value of the string 
  77. * 
  78. *Exceptions: 
  79. * None - overflow is not detected. 
  80. * 
  81. *******************************************************************************/  
  82.   
  83. int __cdecl atoi(  
  84. const char *nptr  
  85. )  
  86. {  
  87. return (int)atol(nptr);  
  88. }  
  89.   
  90. #ifndef _NO_INT64  
  91.   
  92. __int64 __cdecl _atoi64(  
  93. const char *nptr  
  94. )  
  95. {  
  96. int c; /* current char */  
  97. __int64 total; /* current total */  
  98. int sign; /* if ''-'', then negative, otherwise positive */  
  99.   
  100. /* skip whitespace */  
  101. while ( isspace((int)(unsigned char)*nptr) )  
  102. ++nptr;  
  103.   
  104. c = (int)(unsigned char)*nptr++;  
  105. sign = c; /* save sign indication */  
  106. if (c == ''-'' || c == ''+'')  
  107. c = (int)(unsigned char)*nptr++; /* skip sign */  
  108.   
  109. total = 0;  
  110.   
  111. while (isdigit(c)) {  
  112. total = 10 * total + (c - ''0''); /* accumulate digit */  
  113. c = (int)(unsigned char)*nptr++; /* get next char */  
  114. }  
  115.   
  116. if (sign == ''-'')  
  117. return -total;  
  118. else  
  119. return total; /* return result, negated if necessary */  
  120. }  
  121.   
  122. #endif /* _NO_INT64 */  
  123.   
  124.   
  125. #include <msvcrt/errno.h>  
  126. #include <msvcrt/stdlib.h>  
  127. #include <msvcrt/internal/file.h>  
  128. char* _itoa(int value, char* string, int radix)  
  129. {  
  130. char tmp[33];  
  131. char* tp = tmp;  
  132. int i;  
  133. unsigned v;  
  134. int sign;  
  135. char* sp;  
  136.   
  137. if (radix > 36 || radix <= 1)  
  138. {  
  139. __set_errno(EDOM);  
  140. return 0;  
  141. }  
  142.   
  143. sign = (radix == 10 && value < 0);  
  144. if (sign)  
  145. v = -value;  
  146. else  
  147. v = (unsigned)value;  
  148. while (v || tp == tmp)  
  149. {  
  150. i = v % radix;  
  151. v = v / radix;  
  152. if (i < 10)  
  153. *tp++ = i+''0'';  
  154. else  
  155. *tp++ = i + ''a'' - 10;  
  156. }  
  157.   
  158. if (string == 0)  
  159. string = (char*)malloc((tp-tmp)+sign+1);  
  160. sp = string;  
  161.   
  162. if (sign)  
  163. *sp++ = ''-'';  
  164. while (tp > tmp)  
  165. *sp++ = *--tp;  
  166. *sp = 0;  
  167. return string;