Leaked source code of windows server 2003
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.

60 lines
1.7 KiB

  1. #include <windows.h>
  2. /* helper routine that does the main job. */
  3. static void xtoa (
  4. unsigned long val,
  5. char *buf,
  6. unsigned radix,
  7. int is_neg
  8. )
  9. {
  10. char *p; /* pointer to traverse string */
  11. char *firstdig; /* pointer to first digit */
  12. char temp; /* temp char */
  13. unsigned digval; /* value of digit */
  14. p = buf;
  15. if (is_neg) {
  16. /* negative, so output '-' and negate */
  17. *p++ = '-';
  18. val = (unsigned long)(-(long)val);
  19. }
  20. firstdig = p; /* save pointer to first digit */
  21. do {
  22. digval = (unsigned) (val % radix);
  23. val /= radix; /* get next digit */
  24. /* convert to ascii and store */
  25. if (digval > 9)
  26. *p++ = (char) (digval - 10 + 'a'); /* a letter */
  27. else
  28. *p++ = (char) (digval + '0'); /* a digit */
  29. } while (val > 0);
  30. /* We now have the digit of the number in the buffer, but in reverse
  31. order. Thus we reverse them now. */
  32. *p-- = '\0'; /* terminate string; p points to last digit */
  33. do {
  34. temp = *p;
  35. *p = *firstdig;
  36. *firstdig = temp; /* swap *p and *firstdig */
  37. --p;
  38. ++firstdig; /* advance to next two digits */
  39. } while (firstdig < p); /* repeat until halfway */
  40. }
  41. /* Actual functions just call conversion helper with neg flag set correctly,
  42. and return pointer to buffer. */
  43. PSTR ULtoA( unsigned long val, char *buf, int radix )
  44. {
  45. xtoa(val, buf, radix, 0);
  46. return buf;
  47. }