Click here to Skip to main content
14,541,301 members
Rate this:
Please Sign up or sign in to vote.
See more:
A run of the program should prompt the user to enter a number, the base of the number, and the base to convert to the number to, and then print the results of converting the number to decimal, and then the decimal to the output base. Here’s an example of output if the number to be converted is Hex 23B4 to Octal:

So a general process flow is:
⦁ Ask for number to convert
⦁ Ask for base of number
⦁ Ask for base to convert to
⦁ Set decimal to return of “toDecimal” passing the number-in and base-in
⦁ Print message number-in base base-in is decimal decimal-value
⦁ Set number-out to return of “fromDecimal” passing decimal and base-out
⦁ Print message decimal is number-out in base base-out

Outline of the fromDecimal function:
⦁ Define a string number-out and initialize it to an empty string, i.e. ‘’ or “” (this will allow us to concatenate digits to it in a loop)
⦁ Set a quotient variable to the decimal passed in
⦁ Create while loop that checks for quotient great than zero
⦁ Set a remainder variable to quotient mod base-out
⦁ Set next-digit to return of valueToDigit passing in the remainder
⦁ Set a new-quotient variable to quotient integer-division base-out
⦁ Print this loop as: quotient / base-out = new-quotient , next-digit
⦁ Append next digit to front of number-out (e.g. sNumberOut = nextDigit + sNumberOut)
⦁ Set quotient to new-quotient
⦁ (Loop will continue till quotient is zero)
⦁ Return number-out

What I have tried:

# Define Decimal, and Part Two.
def toDecimal(sNumber, nBase):
decimal = 0

# Collect Data for the Conversion.
for i in range(len(sNumber)):

digitPos = len(sNumber) - 1 - i

digit = sNumber[digitPos]

digitVal = digitToValue(digit, nBase)

if (digitVal == -1):
return -1

posVal = nBase ** i

product = digitVal * posVal

print(digit, "x", nBase, "^", i, "=", digitVal, "x", posVal, "=", product)

decimal += product

return decimal

numOut = ""
quotient = decimal
while quotient > 0
quotient = quotient%2

# Print Result
decimalOut = toDecimal(numIn, baseIn)
print(numIn,"base",baseIn,"is decimal",decimalOut)
numberOut = fromDecimal(decimal,baseOut)
print("decimal",decimal,"is",numOut,"in base",baseOut)
Updated 19-Feb-18 23:13pm
Patrice T 19-Feb-18 23:31pm
Describe the problem.
Member 13686192 20-Feb-18 0:12am
function that simulates the long division method of converting a decimal to another base. It will need to receive an integer decimal value, and an integer base to convert it to, and will need to return a string value representing the converted value. How would i go about this?
Patrice T 20-Feb-18 4:00am
Use Improve question to update your question.
So that everyone can pay attention to this information.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Are you the same person that posted How do I create a function that simulates the long division method of converting a decimal to another base[^], because the description is exactly the same? If so please delete the duplicate profile. If not, then see the answers there.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100