Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
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.
 
 
 
 

30 lines
591 B

  1. #include "fmt.h"
  2. #include "haveinline.h"
  3. static inline char tohex(char c) {
  4. return (char)(c>=10?c-10+'a':c+'0');
  5. }
  6. size_t fmt_xlong(char *dest,unsigned long i) {
  7. register unsigned long len,tmp;
  8. /* first count the number of bytes needed */
  9. for (len=1, tmp=i; tmp>15; ++len) tmp>>=4;
  10. if (dest)
  11. for (tmp=i, dest+=len; ; ) {
  12. *--dest = tohex(tmp&15);
  13. if (!(tmp>>=4)) break;
  14. }
  15. return len;
  16. }
  17. #ifdef UNITTEST
  18. #include <assert.h>
  19. #include <string.h>
  20. int main() {
  21. char buf[100];
  22. assert(fmt_xlong(buf,0x12345)==5 && !memcmp(buf,"12345",5));
  23. return 0;
  24. }
  25. #endif