2001-02-02 17:54:47 +00:00
|
|
|
#include "fmt.h"
|
2001-11-24 20:11:41 +00:00
|
|
|
#include "haveinline.h"
|
2001-02-02 17:54:47 +00:00
|
|
|
|
|
|
|
static inline char tohex(char c) {
|
2014-03-14 00:24:25 +00:00
|
|
|
return (char)(c>=10?c-10+'a':c+'0');
|
2001-02-02 17:54:47 +00:00
|
|
|
}
|
|
|
|
|
2006-11-07 17:56:05 +00:00
|
|
|
size_t fmt_xlong(char *dest,unsigned long i) {
|
2001-02-02 17:54:47 +00:00
|
|
|
register unsigned long len,tmp;
|
|
|
|
/* first count the number of bytes needed */
|
|
|
|
for (len=1, tmp=i; tmp>15; ++len) tmp>>=4;
|
|
|
|
if (dest)
|
2001-04-23 13:41:17 +00:00
|
|
|
for (tmp=i, dest+=len; ; ) {
|
2001-02-02 17:54:47 +00:00
|
|
|
*--dest = tohex(tmp&15);
|
2001-04-23 13:41:17 +00:00
|
|
|
if (!(tmp>>=4)) break;
|
|
|
|
}
|
2001-02-02 17:54:47 +00:00
|
|
|
return len;
|
|
|
|
}
|
2017-04-30 20:16:09 +00:00
|
|
|
|
|
|
|
#ifdef UNITTEST
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
char buf[100];
|
|
|
|
assert(fmt_xlong(buf,0x12345)==5 && !memcmp(buf,"12345",5));
|
2017-05-13 22:52:21 +00:00
|
|
|
return 0;
|
2017-04-30 20:16:09 +00:00
|
|
|
}
|
|
|
|
#endif
|