tedj@hpcilzb.HP.COM (Ted Johnson) (08/23/88)
Does anyone know of an algorithm to convert a positive integer N into its base 3 representation? (e.g., 14 in base 10 is 112 in base 3). Any textbook/journal pointers are appreciated! (Source code is appreciated even more :-) -Ted
tedj@hpcilzb.HP.COM (Ted Johnson) (08/24/88)
Never mind! I just got a solution. I turned out to be a lot easier than I expected. :-} -Ted
djones@megatest.UUCP (Dave Jones) (08/24/88)
From article <650003@hpcilzb.HP.COM>, by tedj@hpcilzb.HP.COM (Ted Johnson): > Does anyone know of an algorithm to convert a positive integer N into > its base 3 representation? (e.g., 14 in base 10 is 112 in base 3). > > Any textbook/journal pointers are appreciated! (Source code is > appreciated even more :-) > > -Ted /* Always happy to help, (if it's this easy). */ #define THREES_MAX 21 /* A number of chars big enough to represent ** any int in base three. Assuming here 32 bit ints. */ main(argc, argv) char** argv; { char threes_rep[THREES_MAX]; base3(atoi(argv[1]), threes_rep); printf("%s\n", threes_rep); exit(0); } #define digit(num) ((char)(num + '0')) base3(num, result) int num; char* result; { int power = 0; char backwards[THREES_MAX]; while(num != 0) { backwards[power++] = digit(num % 3); num = num / 3; } { int rpower = 0; while(--power >= 0) { result[rpower++] = backwards[power]; } result[rpower] = '\0'; } }
wu@spot.Colorado.EDU (WU SHI-KUEI) (08/24/88)
In article <650003@hpcilzb.HP.COM> tedj@hpcilzb.HP.COM (Ted Johnson) writes: >Does anyone know of an algorithm to convert a positive integer N into >its base 3 representation? (e.g., 14 in base 10 is 112 in base 3). 'bc' with obase = 3 does a dandy job. A guest here. In real life Carl Brandauer nbires!bdaemon!carl
ark@alice.UUCP (08/24/88)
In article <650003@hpcilzb.HP.COM>, tedj@hpcilzb.UUCP writes: > Does anyone know of an algorithm to convert a positive integer N into > its base 3 representation? (e.g., 14 in base 10 is 112 in base 3). To convert a number to base N: void convert(unsigned x, unsigned N) { if (x >= N) convert(x/N, N); printdigit(x%N); } -- --Andrew Koenig ark@europa.att.com
davidsen@steinmetz.ge.com (William E. Davidsen Jr) (08/25/88)
In article <650003@hpcilzb.HP.COM> tedj@hpcilzb.HP.COM (Ted Johnson) writes: >Does anyone know of an algorithm to convert a positive integer N into >its base 3 representation? (e.g., 14 in base 10 is 112 in base 3). This is one of the problems given in _intro to programming_ courses, but here's the C code for any positive integer to any base 1..16. Note I'm typing it in, so look for typos before you tell me it doesn't work. print_to_base(val, base) int val, base; { register int a; static char *digits = "0123456789ABCDEF"; if (a = val/base) print_to_base(a, base); putchar(digits[val%base]); } I usually let students modify it to handle negative values, fixed width, etc. I use a form of it to output things to binary, writing to an internal string and returning the address of the string. Disclamer: at least ten people will tell me there's a better way to do the general case, or to do the base three case only. At least twice as many people will tell me there's a better way than tried to help the original poster. If my company has an opinion they don't tell me. -- bill davidsen (wedu@ge-crd.arpa) {uunet | philabs | seismo}!steinmetz!crdos1!davidsen "Stupidity, like virtue, is its own reward" -me
mouse@mcgill-vision.UUCP (der Mouse) (08/27/88)
In article <650003@hpcilzb.HP.COM>, tedj@hpcilzb.HP.COM (Ted Johnson) writes: > [algorithm for converting between bases] > (Source code is appreciated even more :-) I have just submitted to comp.sources.misc a program for converting between arbitrary bases (within certain limits). If you want such a thing, look for it there. der Mouse old: mcgill-vision!mouse new: mouse@larry.mcrcim.mcgill.edu
Chuck_M_Grandgent@cup.portal.com (08/28/88)
tedj@hpcilzb.HP.COM (Ted Johnson) wants to know > Does anyone know of an algorithm to convert a positive integer N into > its base 3 representation? (e.g., 14 in base 10 is 112 in base 3). > Any textbook/journal pointers are appreciated! (Source code is > appreciated even more :-) You may wish to look into a FORTH system, or its code. FORTH has a system variable called BASE, and by storing the desired base into BASE, all terminal I/O (numbers) are carried out in the requested base. For example 17 BASE ! (storing 17 into BASE) will cause all subsequent terminal I/O, plus the arithmetic operators to do their stuff in base 17. Source is available for many FORTH systems. ============ sun!portal!cup.portal.com!chuck_m_grandgent=========== ============ AEG Modicon, Industrial Automation Systems Group =====
chris@mimsy.UUCP (Chris Torek) (08/31/88)
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 #!