libowfat/fmt/fmt_8long.c

26 lines
547 B
C
Raw Normal View History

2001-02-02 17:54:47 +00:00
#include "fmt.h"
2006-11-07 17:56:05 +00:00
size_t fmt_8long(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 */
2001-08-23 19:01:52 +00:00
for (len=1, tmp=i; tmp>7; ++len) tmp>>=3;
2001-02-02 17:54:47 +00:00
if (dest)
2001-04-23 13:41:17 +00:00
for (tmp=i, dest+=len; ; ) {
2014-03-14 00:24:25 +00:00
*--dest = (char)((tmp&7)+'0');
2001-04-23 13:41:17 +00:00
if (!(tmp>>=3)) break;
}
2001-02-02 17:54:47 +00:00
return len;
}
#ifdef UNITTEST
#include <assert.h>
#include <string.h>
int main() {
char buf[100];
assert(fmt_8long(buf,012345)==5 && !memcmp(buf,"12345",5));
assert(fmt_8long(buf,0)==1 && !memcmp(buf,"0",1));
return 0;
}
#endif