Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Decimal number from whatever base the user inputs c++

  1. Sep 29, 2011 #1
    Hey all, I am majoring in math and taking this c++ which I am having some trouble with. [Im into paper and pencils :grumpy: ]

    So my assignment is to have a number saved in a string then convert it from a string to an integer (have that done) and finally convert that number to a decimal number from whatever base the user inputs.

    I am lost with the int-base to decimal.

    I was thinking using BASE 2 and convert it which ever base.
    Integer--> convert it to base 2-->user inputs base-->converst BASE 2-->BASE N

    read 6
    convert that to base 2-->110 [4+2+0=6]
    user wants base 2-->print 110

    Im assuming there is an algorithm for converting between bases that I dont know about.

    Any ideas on how to do this and additional help would be really appreciated!!

    ty all!
  2. jcsd
  3. Sep 30, 2011 #2
    If you are somewhat clever you may be able to discover a base converting algorithm on your own.

    With the place value system with base b, we would treat a positive number as a sum some terms. Each term is an integer in the range 0 to b-1, multiplied by bi, where i is an integer designating place. For example:

    d2*b2 + d1*b1 + d0*b0

    Hint: The largest bi with a non-zero digit di will be less than the number. If bi is greater than than the number you want to represent, then the digit that multiplies bi must be zero, because if we added 1*b^i, the sum would already be too large.
  4. Sep 30, 2011 #3
    Thats easy I got that..my problem is that I dont know how to program that!! What if its place 1 2 4..but for other 1 5 25

    thats where Im confused
  5. Sep 30, 2011 #4
    Did you read my hint?

    so let's say we have the number 333 in base 5

    3 * 5^2 + 3 * 5^1 + 3 * 5^0

    and we wanted to convert that to base 2.

    How many times does 2^7 go into this number? How many times does 2^6 go into this number? What would we have left over?

    You might attempt to convert some numbers between bases using pencil and paper, and then see if you are able to describe a method that will always work.
  6. Sep 30, 2011 #5
    the hint makes sense..but

    so let's say we have the number 333 in base 5

    3 * 5^2 + 3 * 5^1 + 3 * 5^0

    and we wanted to convert that to base 2.

    How many times does 2^7 go into this number? How many times does 2^6 go into this number? What would we have left over?

    I dont see the relevance in that..wouldnt it be easier to just do base 2 and then convert to the base that user input
  7. Sep 30, 2011 #6


    User Avatar
    Science Advisor

    Hey Jkohn and welcome to the forums.

    For anything involving base you need to use the DIV/MOD algorithm.

    Essentially you run a loop where you get the modulus in some base, subtract that, and then divide and repeat the process until the whole number is gone. As an example:

    Lets say you have 123. First you get modulus base 10 which is 3. Store that as A = 3. Subtract 3 from 123 = 120. Divide by 10 = 12. Again do a modulus 10 which gives B = 2. Subtract 2 from 12 = 10. Divide by 10 = 1. Find the modulus base 10 which gives C = 1. Subtract 1 which gives 0. Since we have zero, we stop.

    Then we reconstruct the number by going backwards, by printing "CBA" as our number base 10.

    If you want to do this for any base (even ones that are variable, i.e. you can have variable bases for different digits), you use the same idea.
  8. Sep 30, 2011 #7


    User Avatar
    Homework Helper

    Your approach converting the input string to an integer first, then converting the integer to an output string is the most straight forwad solution. Trying to directly convert one string to another would be complex, you'd end up having to implement some string based math operators.

    As mentioned above, the div / mod approach will get you the output string, but in reverse order. Also as mentioned above, you could raise the base number (multiply it by itself), also as an integer until it exceeded your input integer value, keeping track of the iteration (the power you raised the base to), then reduced the power by one (divide the powered integer by the base), then use div / mod like algorithm to get the most significan digits of the output string first.
  9. Sep 30, 2011 #8
    I just realized the assignment is BINARY --> DECIMAL
    input A: 110
    input B: BASE 2
    print: 6

    im so damn lost! computers so ehh
  10. Sep 30, 2011 #9
    what im trying to do is read for each case up to base 10..and then using the ammount of characters
    res = x^ammnt of char-1

    do that up to 0

    i just have no idea how to code that!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook