Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1683380
  • 博文数量: 347
  • 博客积分: 9328
  • 博客等级: 中将
  • 技术积分: 2680
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-29 23:45
文章分类

全部博文(347)

文章存档

2016年(1)

2013年(4)

2012年(207)

2011年(85)

2010年(50)

分类: C/C++

2012-04-12 14:39:04


点击(此处)折叠或打开

  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. #include <cruntime.h>
  11. #include <stdlib.h>
  12. #include <ctype.h>

  13. /***
  14. *long atol(char *nptr) - Convert string to long
  15. *
  16. *Purpose:
  17. * Converts ASCII string pointed to by nptr to binary.
  18. * Overflow is not detected.
  19. *
  20. *Entry:
  21. * nptr = ptr to string to convert
  22. *
  23. *Exit:
  24. * return long int value of the string
  25. *
  26. *Exceptions:
  27. * None - overflow is not detected.
  28. *
  29. *******************************************************************************/

  30. long __cdecl atol(
  31. const char *nptr
  32. )
  33. {
  34. int c; /* current char */
  35. long total; /* current total */
  36. int sign; /* if ''-'', then negative, otherwise positive */

  37. /* skip whitespace */
  38. while ( isspace((int)(unsigned char)*nptr) )
  39. ++nptr;

  40. c = (int)(unsigned char)*nptr++;
  41. sign = c; /* save sign indication */
  42. if (c == ''-'' || c == ''+'')
  43. c = (int)(unsigned char)*nptr++; /* skip sign */

  44. total = 0;

  45. while (isdigit(c)) {
  46. total = 10 * total + (c - ''0''); /* accumulate digit */
  47. c = (int)(unsigned char)*nptr++; /* get next char */
  48. }

  49. if (sign == ''-'')
  50. return -total;
  51. else
  52. return total; /* return result, negated if necessary */
  53. }


  54. /***
  55. *int atoi(char *nptr) - Convert string to long
  56. *
  57. *Purpose:
  58. * Converts ASCII string pointed to by nptr to binary.
  59. * Overflow is not detected. Because of this, we can just use
  60. * atol().
  61. *
  62. *Entry:
  63. * nptr = ptr to string to convert
  64. *
  65. *Exit:
  66. * return int value of the string
  67. *
  68. *Exceptions:
  69. * None - overflow is not detected.
  70. *
  71. *******************************************************************************/

  72. int __cdecl atoi(
  73. const char *nptr
  74. )
  75. {
  76. return (int)atol(nptr);
  77. }

  78. #ifndef _NO_INT64

  79. __int64 __cdecl _atoi64(
  80. const char *nptr
  81. )
  82. {
  83. int c; /* current char */
  84. __int64 total; /* current total */
  85. int sign; /* if ''-'', then negative, otherwise positive */

  86. /* skip whitespace */
  87. while ( isspace((int)(unsigned char)*nptr) )
  88. ++nptr;

  89. c = (int)(unsigned char)*nptr++;
  90. sign = c; /* save sign indication */
  91. if (c == ''-'' || c == ''+'')
  92. c = (int)(unsigned char)*nptr++; /* skip sign */

  93. total = 0;

  94. while (isdigit(c)) {
  95. total = 10 * total + (c - ''0''); /* accumulate digit */
  96. c = (int)(unsigned char)*nptr++; /* get next char */
  97. }

  98. if (sign == ''-'')
  99. return -total;
  100. else
  101. return total; /* return result, negated if necessary */
  102. }

  103. #endif /* _NO_INT64 */


  104. #include <msvcrt/errno.h>
  105. #include <msvcrt/stdlib.h>
  106. #include <msvcrt/internal/file.h>
  107. char* _itoa(int value, char* string, int radix)
  108. {
  109. char tmp[33];
  110. char* tp = tmp;
  111. int i;
  112. unsigned v;
  113. int sign;
  114. char* sp;

  115. if (radix > 36 || radix <= 1)
  116. {
  117. __set_errno(EDOM);
  118. return 0;
  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. return string;
  143. }

阅读(916) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~