Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
2.8 KiB

  1. /***
  2. *xtoa.c - convert integers/longs to ASCII string
  3. *
  4. * Copyright (c) 1989-1992, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * The module has code to convert integers/longs to ASCII strings. See
  8. *
  9. *Revision History:
  10. * 06-06-89 PHG Module created, based on asm version
  11. * 03-06-90 GJF Fixed calling type, added #include <cruntime.h> and
  12. * fixed copyright.
  13. * 03-23-90 GJF Made xtoa() _CALLTYPE4.
  14. * 09-27-90 GJF New-style function declarators.
  15. * 01-21-91 GJF ANSI naming.
  16. *
  17. *******************************************************************************/
  18. #include <cruntime.h>
  19. #include <stdlib.h>
  20. #include <limits.h>
  21. /***
  22. *char *_itoa, *_ltoa, *_ultoa(val, buf, radix) - convert binary int to ASCII
  23. * string
  24. *
  25. *Purpose:
  26. * Converts an int to a character string.
  27. *
  28. *Entry:
  29. * val - number to be converted (int, long or unsigned long)
  30. * int radix - base to convert into
  31. * char *buf - ptr to buffer to place result
  32. *
  33. *Exit:
  34. * fills in space pointed to by buf with string result
  35. * returns a pointer to this buffer
  36. *
  37. *Exceptions:
  38. *
  39. *******************************************************************************/
  40. /* helper routine that does the main job. */
  41. static void _CALLTYPE4 xtoa (
  42. unsigned long val,
  43. char *buf,
  44. unsigned radix,
  45. int is_neg
  46. )
  47. {
  48. char *p; /* pointer to traverse string */
  49. char *firstdig; /* pointer to first digit */
  50. char temp; /* temp char */
  51. unsigned digval; /* value of digit */
  52. p = buf;
  53. if (is_neg) {
  54. /* negative, so output '-' and negate */
  55. *p++ = '-';
  56. val = (unsigned long)(-(long)val);
  57. }
  58. firstdig = p; /* save pointer to first digit */
  59. do {
  60. digval = (unsigned) (val % radix);
  61. val /= radix; /* get next digit */
  62. /* convert to ascii and store */
  63. if (digval > 9)
  64. *p++ = (char) (digval - 10 + 'a'); /* a letter */
  65. else
  66. *p++ = (char) (digval + '0'); /* a digit */
  67. } while (val > 0);
  68. /* We now have the digit of the number in the buffer, but in reverse
  69. order. Thus we reverse them now. */
  70. *p-- = '\0'; /* terminate string; p points to last digit */
  71. do {
  72. temp = *p;
  73. *p = *firstdig;
  74. *firstdig = temp; /* swap *p and *firstdig */
  75. --p;
  76. ++firstdig; /* advance to next two digits */
  77. } while (firstdig < p); /* repeat until halfway */
  78. }
  79. /* Actual functions just call conversion helper with neg flag set correctly,
  80. and return pointer to buffer. */
  81. char * _CALLTYPE1 _itoa (
  82. int val,
  83. char *buf,
  84. int radix
  85. )
  86. {
  87. if (radix == 10 && val < 0)
  88. xtoa((unsigned long)val, buf, radix, 1);
  89. else
  90. xtoa((unsigned long)(unsigned int)val, buf, radix, 0);
  91. return buf;
  92. }
  93. char * _CALLTYPE1 _ltoa (
  94. long val,
  95. char *buf,
  96. int radix
  97. )
  98. {
  99. xtoa((unsigned long)val, buf, radix, (radix == 10 && val < 0));
  100. return buf;
  101. }
  102. char * _CALLTYPE1 _ultoa (
  103. unsigned long val,
  104. char *buf,
  105. int radix
  106. )
  107. {
  108. xtoa(val, buf, radix, 0);
  109. return buf;
  110. }