Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I convert from base n to base 10 without using strings or arrays(where n is an input between 2 and 9)? The number being converted is a long long integer type.

The code that I have tried returns the same number (num) that was input into the function.

What I have tried:

int calcBase10(long long num, int base)
{
int base10;
int quotient;
int remainder;

if(base == 10)
{
base10 = num;
return(base10);
}

quotient = num / base;
remainder = num % base;
if(quotient == 0)
{
base10 = remainder;
}
else
{
base10 = (base * calcBase10(quotient, base) + remainder);
}
return(base10);
}
Posted
Updated 24-Mar-16 10:24am
v2
Comments
Patrice T 24-Mar-16 3:55am    
And what is the problem ?
Member 12413627 24-Mar-16 12:54pm    
this function is only returning the original input number for some reason, i dont know why.
Patrice T 24-Mar-16 13:03pm    
Use Improve question to update your question.
Matt T Heffron 24-Mar-16 13:15pm    
Of course!
See my Solution below.
Patrice T 24-Mar-16 13:25pm    
No, the OP have numbers encoded in base10 that are in fact another base.
if you call calcBase10(101, 2), the OP wants 5 as result.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
 
Share this answer
 
Comments
Matt T Heffron 24-Mar-16 13:16pm    
OP's problem is more fundamental.
The concept isn't understood.
Of course it is the same number output as input!
Any integer (e.g., long long) is a pure number, that is internally represented in binary (base 2).
It isn't a base 10, base 3, etc. number until it is converted to/from some other representation (printed or input).
You can write a function that takes a long long value and an int base and returns a string representing that value as a base base number.
Likewise you can write a function that does the opposite, take a string and a base and returns the value having parsed the string as representing a base base number.
 
Share this answer
 
Comments
Patrice T 24-Mar-16 13:26pm    
No, the OP have numbers encoded in base10 that are in fact another base.
if you call calcBase10(101, 2), the OP wants 5 as result.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900