14,270,343 members
Rate this:
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:

```Enter a number: 23b4
Enter base from: 16
Enter base to convert to: 8
4 x 16 ^ 0 = 4 x 1 = 4
B x 16 ^ 1 = 11 x 16 = 176
3 x 16 ^ 2 = 3 x 256 = 768
2 x 16 ^ 3 = 2 x 4096 = 8192
23b4 base 16 is decimal 9140

9140 / 8 = 1142 , 4
1142 / 8 = 142 , 6
142 / 8 = 17 , 6
17 / 8 = 2 , 1
2 / 8 = 0 , 2
decimal 9140 is 21664 in base 8

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:

```def digitToValue(digit, base):
value = 0
if digit >= '0' and digit <= '9':
value = int(digit)
elif digit >= 'A' and digit <= 'F':

if digit == 'A':
value = 10

elif digit == 'B':
value = 11

elif digit == 'C':
value = 12

elif digit == 'D':
value = 13

elif digit == 'E':
value = 14

else:
value = 15
else:
print("Invalid digit!")
return -1

if value < 0 or value >= base:
print("Invalid digit for base!")
return -1

return value

def toDecimal (sNumber, nBase):
decimal = 0

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

numberIn = input("Enter a number: ")
baseIn = input("Enter base from: ")
convertTo = input("Enter base to convert to: ")
decimalOut = toDecimal(numberIn, baseIn)
print(decimalOut)```
Posted
Updated 19-Feb-18 9:38am
v2
PIEBALDconsult 19-Feb-18 0:16am

Simulate?

Rate this:

## Solution 1

The following code fixes the bugs your one has. Now you have to complete it.
```#  ...
def toDecimal (sNumber, nBase):
decimal = 0

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 #  your return statement had wrong indentation

numberIn = input("Enter a number: ")
baseIn = input("Enter base from: ")
convertTo = input("Enter base to convert to: ")
decimalOut = toDecimal(str(numberIn), baseIn) # added the call to str function
print(decimalOut)```
Rate this:

## Solution 2

def toDecimal (sNumber, nBase):
decimal = 0

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

numberIn = input("Enter a number: ")
baseIn = int(input("Enter base from: "))
convertTo = int(input("Enter base to convert to: "))
decimalOut = toDecimal(numberIn, baseIn)
print(numberIn, " base " , baseIn, " is decimal ", decimalOut)
fromDecimal = " "
numberOut = fromDecimal(decimal, baseOut)
print("decimal ", decimal, " is " , numberOut , " in base ", baseOut)
Patrice T 19-Feb-18 12:35pm

Is it your solution or new code withy new problems?
Member 13684472 19-Feb-18 12:57pm

i just dont know how to get it like this:
Enter a number: 23b4
Enter base from: 16
Enter base to convert to: 8
4 x 16 ^ 0 = 4 x 1 = 4
B x 16 ^ 1 = 11 x 16 = 176
3 x 16 ^ 2 = 3 x 256 = 768
2 x 16 ^ 3 = 2 x 4096 = 8192
23b4 base 16 is decimal 9140

9140 / 8 = 1142 , 4
1142 / 8 = 142 , 6
142 / 8 = 17 , 6
17 / 8 = 2 , 1
2 / 8 = 0 , 2
decimal 9140 is 21664 in base 8

because when ever i run it it looks like this:
Enter a number: 23b4
Enter base from: 16
Enter base to convert to: 8
4 x 16 ^ 0 = 4 x 1 = 4
Invalid digit!
23b4 base 16 is decimal -1
Member 13686192 19-Feb-18 22:49pm

Hey, im also stuck on the part with division. Can you show me how is done?

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

Top Experts
Last 24hrsThis month
 OriginalGriff 619 Patrice T 390 Maciej Los 310 Richard Deeming 150 Richard MacCutchan 130
 OriginalGriff 4,210 Maciej Los 1,738 Patrice T 1,580 MadMyche 1,480 Richard MacCutchan 1,041

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