Python Forum
Convert number to a different base and make calculations
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert number to a different base and make calculations
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
what about hex?
Reply
#4
(Dec-07-2018, 07:25 PM)frequency Wrote: what about hex?

That would be another useful function. They're all built-ins.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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?
Reply
#6
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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
Reply
#8
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).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
"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)?
Reply
#10
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to convert every even number in a list to odd? Bruizeh 4 3,758 Aug-27-2021, 03:04 AM
Last Post: naughtyCat
  Weird problem with reading from file and performing calculations pineapple999 1 2,999 Jul-25-2019, 01:30 AM
Last Post: ichabod801
  Convert string to a specific number of bytes artblinked 1 2,429 Mar-28-2019, 08:43 PM
Last Post: Larz60+
  Prime numbers calculations frequency 3 2,995 Nov-27-2018, 07:07 PM
Last Post: micseydel
  Calendar calculations frequency 10 5,598 Nov-13-2018, 07:34 PM
Last Post: frequency
  dc circuit calculations michaelj 1 2,324 Nov-01-2017, 09:19 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020