Thread: base conversions

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    St. Petersburg, FL
    Posts
    35

    base conversions

    Is it possible to have one algorithm to convert to any base (at least 2-12) starting from base 10, or will i need a separate one for each base?
    Last edited by kbat82; 12-10-2008 at 01:15 AM. Reason: added "starting from base 10"

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    First outline your algorithm(s) for converting to bases 2 and 3.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    St. Petersburg, FL
    Posts
    35
    Well, I have base 2, but it's 2am so I'll work on base 3 tomorrow

    Code:
    void to_binary(unsigned long num, int base) {  
    	/* recursive function */
    
    	int remainder;
    
    	remainder = num % 2;
    
    	if (num >= 2)
    
    		to_binary(num / 2);
    
    	putchar( remainder ? '1' : '0');
    
    	return;
    
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    After working on base 3, compare the algorithms and see how you can combine them. For bases no greater than 10 it is straightforward, but you'll have to think a little about numerals for bases above 10 (but don't worry, it is still easy).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    St. Petersburg, FL
    Posts
    35

    solved!

    Yea, it wasn't so difficult I suppose. I made it general for any base, but I don't know how all the number systems actually work. I guess as long as they follow the same rules as hex then this would work.

    Any suggestions?

    Code:
    #include <stdio.h>
    
    void to_base_n(unsigned long, int);
    
    int main(void) {
    
    	unsigned long number;
    	int base;
    
    	printf("Enter an integer and a desired base (q to quit):\n");
    
    	while (scanf("&#37;lu %d", &number, &base) == 2) {
    		printf("Base-%d Equivalent: ", base);
    
    		to_base_n(number, base);
    
    		putchar('\n');
    
    		printf("Enter an integer and a desired base (q to quit):\n");
    	}
    
    	printf("Done.\n");
    	return 0;
    }
    
    void to_base_n(unsigned long num, int base) {  
    	/* recursive function */
    
    	int remainder = num % base;
    
    	if (num >= base)
    		to_base_n(num / base, base);
    
    	if (remainder > 10)
    		remainder += 54;
    	else 
    		remainder += 48;
    
    	putchar(remainder);
    	
    	return;
    }
    Last edited by kbat82; 12-10-2008 at 12:54 PM. Reason: noticed an error in the code :)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Base Converter Part 2
    By encyclopedia23 in forum C Programming
    Replies: 2
    Last Post: 12-30-2006, 02:42 PM
  3. Bit Computation Program
    By cisokay in forum C++ Programming
    Replies: 6
    Last Post: 05-13-2005, 09:32 PM
  4. How to change number data from base 256 to base 16?
    By ooosawaddee3 in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2002, 12:19 AM
  5. Virtual Base Class & Constructor :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 03:14 PM

Tags for this Thread