base conversions

This is a discussion on base conversions within the C Programming forums, part of the General Programming Boards category; Is it possible to have one algorithm to convert to any base (at least 2-12) starting from base 10, or ...

  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 12:15 AM. Reason: added "starting from base 10"

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    First outline your algorithm(s) for converting to bases 2 and 3.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,973
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 11:54 AM. 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, 01: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-04-2002, 11:19 PM
  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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21