Thread: converting numbers from one base to another

  1. #1
    partnole
    Guest

    Question converting numbers from one base to another

    Hello, I need help to write a progam which converts any base number to any other base.
    For example: if the input base is 2 and the value is 10000110, then the value in base 10 136, and if the input base is 16 and the value is FF, then the value in base 2 is 11111111.
    Thank you, as all help will be appreciated.

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Post what you've come up with so far.
    zen

  3. #3
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    The typical way is to divide and find the remainder, so like
    to convert 15 in base 10 to base 2

    and then putting the values of the ramainder together
    15/2 = 7R1
    7/2 = 3R1
    3/2 = 1R1
    1/2 = 0R1

    1111

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Well, does this have to work with arbitrary bases? Because you can natively read and write ints in hex, decimal, or octal representations. Anything else, you'd need to code yourself.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    you will also need to assign symbols to represent values greater than 9 if base is greater than 10. Also the values of the digits in any base other than 10 need to be stored as char not as int, especially if base is > 10. But that's probably obvious.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting Base 10 numbers to Base 2, 8, and 16
    By killcapital in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 10:05 AM
  2. converting to base 10
    By xp5 in forum C Programming
    Replies: 11
    Last Post: 08-31-2007, 02:28 AM
  3. Binary Numbers and Base 2 Calculation for Longs
    By SlyMaelstrom in forum Tech Board
    Replies: 8
    Last Post: 11-09-2005, 08:30 PM
  4. converting numbers to words
    By dionys in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 09:34 AM
  5. How do you represent non-integer base numbers?
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 04-08-2004, 01:09 PM