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.
//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