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.

46 lines
1.0 KiB

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