You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
591 B
C

24 years ago
#include "fmt.h"
#include "haveinline.h"
24 years ago
static inline char tohex(char c) {
return (char)(c>=10?c-10+'a':c+'0');
24 years ago
}
size_t fmt_xlong(char *dest,unsigned long i) {
24 years ago
register unsigned long len,tmp;
/* first count the number of bytes needed */
for (len=1, tmp=i; tmp>15; ++len) tmp>>=4;
if (dest)
24 years ago
for (tmp=i, dest+=len; ; ) {
24 years ago
*--dest = tohex(tmp&15);
24 years ago
if (!(tmp>>=4)) break;
}
24 years ago
return len;
}
#ifdef UNITTEST
#include <assert.h>
#include <string.h>
int main() {
char buf[100];
assert(fmt_xlong(buf,0x12345)==5 && !memcmp(buf,"12345",5));
return 0;
}
#endif