• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

math: converting between numbers with different bases

zhena

Senior member
hey guys,
interesting problem i ran upon...

converting from base 10 to say octal, bin, hex is easy
and visa versa.

but with regards to programming shouldn't we be able to write a general procedure that can convert a number from any base to any other base?

i tried to do this, and thought it was fairly simple but got stuck really fast?

anybody have any idea's?

so to make myself clearer

say i have a procedure Convert (num, base, toBase)

x = Convert (5, 10, 2)
print (x)

should print 101

any idea's guys?
 
Welcome to The Homework Channel.

First of all, such a function would have to take STRINGS not NUMBERS. Simply because numbers are always represented in binary in a computer.

In short, what you do first is scan the input string left to right, accumulating the numerical value from each digit:

x = 0
position = 0

while ( not at end of inputstring )
{
x = base * x + digitvalue(inputstring[position])
increment position
}

Then you have the input value in X, in whatever representation your computer uses.

Now reverse the procedure in the other base:

position = 0

while x > 0
{
outputstring[position] = makedigit( x modulo newbase )
x = x / newbase
increment position
}

Now you got the converted number, but it's right to left.

Not exactly high tech, is it? Computer Science, Beginner's Course, 3rd homework. Or so. You're welcome.

Figuring out the "digitvalue" and "makedigit" subfunctions (extracting a digit value from a character, and making a digit value into a character, respectively) are left to you. So is understanding why these two don't need to know the base.

regards,
Peter
 
I know how to do that!!!


It's easy.

They way the bast 10 system works is the 1's place # is (1 x (10 ^ 0))
or 1
the next place is (1 x (10 ^ 1)), or 10's place
next is (1 x (10^2)), or 100, hundreds place
etc etc
in any # system, septal for example, each place works like this.
so from 14626 in septal,
we have (6 x (7 ^ 0))+(2 x (7 ^ 1))+(6 x (7 ^ 2))+(4 x ( 7 ^ 3))+(1 * (7^4)) which equeals 4087 in base 10.

It's pretty fundamental, and the way all number systems work.
 
Originally posted by: chocbrucemousse
I know how to do that!!!


It's easy.

They way the bast 10 system works is the 1's place # is (1 x (10 ^ 0))
or 1
the next place is (1 x (10 ^ 1)), or 10's place
next is (1 x (10^2)), or 100, hundreds place
etc etc
in any # system, septal for example, each place works like this.
so from 14626 in septal,
we have (6 x (7 ^ 0))+(2 x (7 ^ 1))+(6 x (7 ^ 2))+(4 x ( 7 ^ 3))+(1 * (7^4)) which equeals 4087 in base 10.

It's pretty fundamental, and the way all number systems work.

Exactly this is what my code example does ...

Input string: "14626", base 7, newbase 10

First loop: Convert string to numeric value

X=0
take characters from input string and accumulate in X:
'1' -> X = X*7 + 1 = 1
'4' -> X = X*7 + 4 = 11
'6' -> X = X*7 + 6 = 83
'2' -> X = X*7 + 2 = 583
'6' -> X = X*7 + 6 = 4087
loop stops ( end of string reached)

X = 4087 now.

Fraction X repeatedly using the new base, putting the reminder into the output string:

X = X / 10 = 408 R 7 -> '7'
X = X / 10 = 40 R 8 -> '8'
X = X / 10 = 4 R 0 -> '0'
X = X / 10 = 0 R 4 -> '4'
loop stops (X is zero)


 
Back
Top