0

I am struggling to come up with an algorithm in C++ that converts a number with a decimal point from a base to any other base. I have successfully been able to write a function that converts numbers without decimal points to any desired base.

double  getBase10 (string number, int CurrentBase){
    double converted =0;
    for (int i =0; i < number.length(); i++){
        converted += pow(CurrentBase, -1 * (number.length() - i -1)) * getNumber(number[i]);
    }
    return double converted;
}

This is the function I have so far as requested by Mooing Duck.

11
  • Can you show the code you have without decimals? I bet it can be trivially expanded for decimals Sep 9, 2014 at 21:49
  • Unrelated, it makes little sense to "convert a number... from a base..." because values don't have bases. Numeric representations such as strings have bases, but numbers themselves don't have bases. So are you converting from a string-represented-number in one base to a string-represented-number in another? Or just from a value to a string-represented-number? Sep 9, 2014 at 21:51
  • Whoa, this converts a string-represented-number in a base and converts that to a value. Misleading because nothing in this code has anything to do with base10. Sep 9, 2014 at 22:05
  • Also is there a reason to not be using stringstream or boost::lexical_cast or somesuch? Sep 9, 2014 at 22:07
  • Could you please provide an example of how you could accomplish this task using stringstream?
    – Alex Clark
    Sep 9, 2014 at 22:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.