numbers in different bases

I'm trying to make my program convert a number specified by the user in base 10 into another base specified by the user. I need to calculate out 6 digits for the converted number. Here is a test example I ran but I'm having trouble coding the math formulas. Any ideas?

45 in base 10 - convert to base 4

4 into 45 = 11, remainder 1
4 into 11 = 2, remainder 3
4 into 2 = 0, remainder 2
4 into 0 = 0, remainder 0
4 into 0 = 0, remainder 0
4 into 0 = 0, remainder 0

result: 45 in base 10 is 000231 in base 4
check: 231 in base 4 =
0 * 4^5 + 0 * 4^4 + 0 * 4^3 + 2 * 4^2 + 3 * 4^1 + 1 * 4^0
= 0 + 0 + 0 + 32 + 12 + 1 = 45
You need the integer operators:

%
remainder: remainder = dividend % radix;
/
integer divide: quotient = dividend / radix;
Use a loop to keep dividing while the dividend is not zero.

Remember also to use roman numerals for the digits.
Use the following simple functions to convert between their textual representation and their integer values:
1
2
3
4
5
6
7
8
9
char todigit( int n )
  {
  return (n + '0');
  }

int tovalue( char c )
  {
  return (c - '0');
  }

Don't forget that "123" is a string and 123 is an integer.

Hope this helps.
I haven't learned everything you mentioned yet. I understand:

% remainder
/ divide
and loop in concept

Any chance you could dumb it down for me? Much appreciated, thank you.
maybe this will help

1
2
3
4
5
6
7
8
9
//Decimal to X base
//Visual C++ 
string^ result;
while(number>0)  //number in base 10
{
  result = number%X + result;  //its not arithemtical "+" dont forget
  number = number/X;
  i++;
}

e.g. we have to convert 23 to base "7" system representation:
23%7=2 , 23/7=3 (result=2)
3%7=3, 3/7=0 (result=3)
so the base 7 representation of 23 is 32

or...

19 to binary...
19%2=1, 19/2=9 (result=1)
9%2=1, 9/2=4 (result=1)
4%2=0, 4/2=2 (result=0)
2%2=0, 2/2=1 (result=0)
1%2=1, 1/2=0 (result=1)

19(10) = 10011(2)
Last edited on
Thank you so much. It seems to work.
Topic archived. No new replies allowed.