File: fmt_xlong.c

package info (click to toggle)
ucspi-tcp-src 0.88-15
  • links: PTS
  • area: non-free
  • in suites: lenny, squeeze
  • size: 1,380 kB
  • ctags: 761
  • sloc: ansic: 6,758; makefile: 854; sh: 312
file content (22 lines) | stat: -rw-r--r-- 438 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "fmt.h"

char tohex(char num) {
  if (num<10)
    return num+'0';
  else if (num<16)
    return num-10+'a';
  else
    return -1;
}

unsigned int fmt_xlong(register char *s,register unsigned long u)
{
  register unsigned int len; register unsigned long q;
  len = 1; q = u;
  while (q > 15) { ++len; q /= 16; }
  if (s) {
    s += len;
    do { *--s = tohex(u % 16); u /= 16; } while(u); /* handles u == 0 */
  }
  return len;
}