xref: /csrg-svn/sys/stand.att/printn.c (revision 48980)
1*48980Sbostic /*-
2*48980Sbostic  * Copyright (c) 1982, 1986 The Regents of the University of California.
3*48980Sbostic  * All rights reserved.
433437Sbostic  *
5*48980Sbostic  * %sccs.include.redist.c%
6*48980Sbostic  *
7*48980Sbostic  *	@(#)printn.c	7.3 (Berkeley) 05/03/91
833437Sbostic  */
933437Sbostic 
1040501Sroot #include "sys/param.h"
1133437Sbostic 
1233437Sbostic /*
1333437Sbostic  * Printn prints a number n in base b.
1433437Sbostic  * We don't use recursion to avoid deep kernel stacks.
1533437Sbostic  */
printn(n,b)1633437Sbostic printn(n, b)
1733437Sbostic 	u_long n;
1833437Sbostic 	int b;
1933437Sbostic {
2033437Sbostic 	register char *cp;
2133437Sbostic 	char prbuf[11];
2233437Sbostic 
2333437Sbostic 	if (b == 10 && (int)n < 0) {
2433437Sbostic 		putchar('-');
2533437Sbostic 		n = (unsigned)(-(int)n);
2633437Sbostic 	}
2733437Sbostic 	cp = prbuf;
2833437Sbostic 	do {
2933437Sbostic 		*cp++ = "0123456789abcdef"[n%b];
3033437Sbostic 		n /= b;
3133437Sbostic 	} while (n);
3233437Sbostic 	do
3333437Sbostic 		putchar(*--cp);
3433437Sbostic 	while (cp > prbuf);
3533437Sbostic }
36