Python Forum
Convert number to a different base and make calculations - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Convert number to a different base and make calculations (/thread-14581.html)

Pages: 1 2


Convert number to a different base and make calculations - frequency - Dec-07-2018

Hello,
Is there any way to make 010(10)*067(8)-->(2).
In this example. "010" will be converted to Decimal and will be multiplied with "067" which will be converted to octal and finally,the result will be converted to binary! Thanks


RE: Convert number to a different base and make calculations - ichabod801 - Dec-07-2018

All integers are stored in based 2. Other bases are only relevant for converting to and from strings. Relevant functions would include int, bin, and oct.


RE: Convert number to a different base and make calculations - frequency - Dec-07-2018

what about hex?


RE: Convert number to a different base and make calculations - ichabod801 - Dec-07-2018

(Dec-07-2018, 07:25 PM)frequency Wrote: what about hex?

That would be another useful function. They're all built-ins.


RE: Convert number to a different base and make calculations - frequency - Dec-08-2018

basehex = input()
sechex = input()
basehexin = int(basehex, 16)
sechexin = int(sechex, 16)
sum = basehexin+sechexin
print(hex(sum))
With this code 2 inputs will be coded to HEX and summed together.
Example!
Input_1=0110101 #(BIN)
Input_2=0143 #(OCT)
Both of these inputs will become HEX.I am told that the calculation will print 098 BUT instead i get a hex 0x110244.What am i doing wrong?


RE: Convert number to a different base and make calculations - ichabod801 - Dec-08-2018

If input one is supposed to made in binary, you should convert it with base 2 on line 3, not base 16. Likewise, input two should be converted with base 8 on line 4 if it is supposed to be entered in octal. Then you print the hex of the sum to output it in hexadecimal.

Note that you should not call your variable sum. 'sum' is also a built-in function, and if you name something else sum it may mess up something else that is trying to use the built-in function.


RE: Convert number to a different base and make calculations - frequency - Dec-13-2018

Thanks for the reply!
def splitter():
	for i in range (len(user_data)):
		if user_data[i]=="+" or user_data[i]=="-" or user_data[i]=="/" or user_data[i]=="*":
			symbol=user_data[i]
	return
I am trying to complete this def to seperate the 2 numbers for my values. Lets say the string is 666(2)+999(8)-->(16)
. How can i seperate the 666 and the 999 as 2 seperate values?
Thanks


RE: Convert number to a different base and make calculations - ichabod801 - Dec-13-2018

This is what I would do. Start with number = 0; numbers = []. Then loop through the characters in the string. If the character is a digit, convert it to an integer, and add it to number times ten for the new value of number. If it's not a digit, append number to numbers, and reset number to zero. At the end, you'll have a list of all the numbers: [666, 2, 999, 8, 16].

(okay, I would actually use the re module, but that's not for beginning homework).


RE: Convert number to a different base and make calculations - frequency - Dec-13-2018

"and add it to number times ten",what do you mean by that? Will it work if i have for example
1010101010(2)+83837(10)?


RE: Convert number to a different base and make calculations - ichabod801 - Dec-13-2018

Oh, good point. That won't work. You can use something similar (just add to a string which you reset when there's a no digit) to get all the numbers as strings. Then you can find the bases and use them to convert the other strings.