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.

54 lines
1.2 KiB

  1. #include "rcpptype.h"
  2. /* Long to ASCII conversion routine - used by print, and those programs
  3. * which want to do low level formatted output without hauling in a great
  4. * deal of extraneous code. This will convert a long value to an ascii
  5. * string in any radix from 2 - 16.
  6. * RETURNS - the number of characters in the converted buffer.
  7. */
  8. static WCHAR digits[] = {
  9. L'0', L'1', L'2', L'3', L'4',
  10. L'5', L'6', L'7', L'8', L'9',
  11. L'a', L'b', L'c', L'd', L'e', L'f'
  12. };
  13. #define BITS_IN_LONG (8*sizeof(long))
  14. int
  15. zltoa(
  16. long aval,
  17. register WCHAR *buf,
  18. int base
  19. )
  20. {
  21. // if unsigned long wont work on your host, you will probably have
  22. // to use signed long and accept this as not working for negative
  23. // numbers.
  24. register unsigned long val;
  25. register WCHAR *p;
  26. WCHAR tbuf[BITS_IN_LONG];
  27. int size = 0;
  28. p = tbuf;
  29. *p++ = L'\0';
  30. if (aval < 0 && base == 10) {
  31. *buf++ = L'-';
  32. val = -aval;
  33. size++;
  34. } else {
  35. val = aval;
  36. }
  37. do {
  38. *p++ = digits[val % base];
  39. } while (val /= base);
  40. while ((*buf++ = *--p) != 0) {
  41. ++size;
  42. }
  43. return(size);
  44. }