1

I'm trying to make a program to convert a number which is in base 6 to decimal(base 10). It doesn't seem to work. Could anyone please help.

a = 75
if a == 0:
    return 0
a = str(a)
result = (a[0])
for i in a[:1]:
    result *= 6
    result += (i)
print result

The result does not work.

Now let's say I try to add 2 base 6 numbers. How do I proceed? I tried to convert each number to base 6 and add them, the answer is not correct. I also tried to add them first and then convert to base 6 but it still doesn't work.

How would I proceed for multiplication as well?

Thanks

Sab ಠ_ಠ
  • 135
  • 1
  • 4
  • 13

2 Answers2

5

You can use int function, with the radix parameter, like this

int(str(number), 6)

For example,

print(int(str(55), 6))
# 35
print(int('10', 6))
# 6
print(int('6', 6))
# ValueError: invalid literal for int() with base 6: '6'
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • But that's decimal to base 6, right? I need the opposite – Sab ಠ_ಠ Apr 04 '14 at 13:44
  • @Sab Nope, it is base 6 to decimal – thefourtheye Apr 04 '14 at 13:44
  • Thanks. But how can I add 2 base 6 numbers or multiply them> – Sab ಠ_ಠ Apr 04 '14 at 13:47
  • @Sab Convert both the numbers to base 10, do whatever operations you want to do and then convert back to base 6 with [this answer](http://stackoverflow.com/a/2267428/1903116) – thefourtheye Apr 04 '14 at 13:49
  • 2
    @Sab the base is just a way of *representing* a number - in Python terms, a rule for str <-> int, rather than an intrinsic part of the number itself. Once you have it in an `int`, you can multiply and add them in the usual way.. Then convert the result back to a string using base 6 to *display* it. The result of calculations are the same regardless of what base you do them in (... provided you stick to whole numbers, but that's another story) - your computer almost certainly uses base 2 internally, for example, but most programs, including Python, display it back to you in base 10 by default. – lvc Apr 04 '14 at 13:59
  • @lvc I got it thanks. Basically what I did was convert it to base 10, then do whatever arithmetic I wanted and took that result converted it back to base 6. – Sab ಠ_ಠ Apr 04 '14 at 14:00
0

The question "How can I add 2 base 6 numbers or multiply them" does not really make sense.

All numbers in a computer is in base 2.

In addition the base is just a representation. So (The number with base 10 representation '75')+(The number with base 10 representation '103')=(The number with base 10 representation '178') does not depend on the base.

Base is a feature when you need to display the number, because then there needs to be a conversion from bits to characters. In most programming languages this is by default to base 10.

So to answer your question "How can I add 2 base 6 numbers or multiply them?" I would say; you don't, you do arithmetic on base 2 numbers. If your Input/Output is in base 6 you need to take care when reading/writing the numbers so that the conversion is to the correct base.

Taemyr
  • 3,407
  • 16
  • 26