nightmaremail

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

fmt_ulong.c (987B)


      1 #include "fmt.h"
      2 
      3 unsigned int fmt_ulong(char *s, unsigned long u)
      4 {
      5   unsigned int len; unsigned long q;
      6   len = 1; q = u;
      7   while (q > 9) { ++len; q /= 10; }
      8   if (s) {
      9     s += len;
     10     do { *--s = '0' + (u % 10); u /= 10; } while(u); /* handles u == 0 */
     11   }
     12   return len;
     13 }
     14 
     15 /* fmt_ulongalphabet is used if you have special numeral needs*/
     16 unsigned int fmt_ulongalphabet(char *s, unsigned long u, char *alphabet, unsigned long alphabetlen)
     17 {
     18   unsigned int len; unsigned long q;
     19   len = 1; q = u;
     20   while (q >= alphabetlen) { ++len; q /= alphabetlen; }
     21   if (s && alphabet) {
     22     s += len;
     23     do { *--s = alphabet[(u % alphabetlen)]; u /= alphabetlen; } while(u); /* handles u == 0 */
     24   }
     25   return len;
     26 }
     27 
     28 unsigned int fmt_ulonglong(char *s, unsigned long long u)
     29 {
     30   unsigned int len; unsigned long long q;
     31   len = 1; q = u;
     32   while (q > 9) { ++len; q /= 10; }
     33   if (s) {
     34     s += len;
     35     do { *--s = '0' + (u % 10); u /= 10; } while(u); /* handles u == 0 */
     36   }
     37   return len;
     38 }