Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!rutgers!uwvax!oddjob!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: algorithm to convert N into base 3 wanted! Message-ID: <13306@mimsy.UUCP> Date: 30 Aug 88 23:42:11 GMT References: <650003@hpcilzb.HP.COM> <477@poseidon.UUCP> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 75 In article <477@poseidon.UUCP> psrc@poseidon.UUCP (Paul S. R. Chisholm) writes: >I realize not everyone has this function yet, but the ANSI draft >defines a function ultoa() > > char* ultoa( unsigned long value, char *string, int radix) > >(you said positive) which converts value to a string for any radix >it's passed. itoa() and ltoa() are similar, but take an integer or >long as their first arguments, respectively. I cannot find these functions anywhere in the May 1988 draft. Perhaps you are thinking of strtol(), strtoul(), and strtod(). For what it is worth, here is a print-radix routine that handles bases 2..16, signed and unsigned, allowing up to 128 bit `long's (if the caller provides that much space): typedef unsigned long expr_t; /* expression type */ typedef /*signed*/ long sexpr_t; /* signed variant */ /* * Print the value `val' in base `base' into the buffer at `p'.'.'emZ value is treated as signed if `sign' is nonzero; if `sign' * The loop is optimised to avoid unsigned division when printing * in octal and hex, since on some machines (e.g., vax) this is */ printradix(p, val, base, sign) register char *p; register expr_t val; register int base; int sign; { register char *d; register expr_t high; char digs[128]; if (sign) { if ((sexpr_t)val < 0) { val = -val; *p++ = '-'; } else if (sign > 0) *p++ = '+'; } d = digs; switch (base) { case 8: do { *d++ = val & 7; } while ((val >>= 3) != 0); break; case 16: do { *d++ = val & 15; } while ((val >>= 4) != 0); break; default: do { high = val / base; *d++ = val - (high * base); } while ((val = high) != 0); break; } while (d > digs) *p++ = "0123456789abcdef"[*--d]; *p = 0; } -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris #!