1

I'm trying to make a function that takes in a number in base 10 and can be changed to any base from 2 though 9. I'm trying to only use the math module and just some simple math.

My code involves strings and I would like to eliminate them, and instead have the function do some math instead of using the numbers in a string. Also I'm trying to make the output integers.

def conver(n,b):
    digits = ('0123456789')
    ans = ""
    while n > 0:
        ans = digits[n%b] + ans
        n //= b
    return ans

For example the user could in put in the values (14,2) and get the output 1110

  • What is the purpose of `digits[n%b]`? – Oliver Charlesworth Dec 11 '17 at 21:15
  • `digits[n%b]` can be replaced by `chr(48 + n%b)` therefore `digits` can be removed then. – Michael Butscher Dec 11 '17 at 21:17
  • 1
    You don't want to convert a "base 10 number". You want to convert a Python integer (an object of type `int`, or possibly of type `long` if you're using Python 2). That's not the same thing. One way to _specify_ a Python `int` is via an integer literal written in the usual decimal notation, but that doesn't make the resulting `int` object a "base 10 number" in any sense. – Mark Dickinson Dec 11 '17 at 21:18
  • 1
    Converting bases is only a matter of string representation, as far as I'm aware of. All numerical values are actually stored in base 2. If you want to convert them, you'll actually get the string representation for printing, but the numerical value will still be stored in base 2 :-) – Yotam Salmon Dec 11 '17 at 21:19
  • Related: https://stackoverflow.com/questions/2267362/how-to-convert-an-integer-in-any-base-to-a-string – Robᵩ Dec 11 '17 at 21:24
  • @nick: To clarify, what do you want the type of your output to be? When you say output `1110`, what's the desired Python type of that? `int`? Or `str`? – Mark Dickinson Dec 11 '17 at 21:25
  • sorry, I meant `int` –  Dec 11 '17 at 21:26
  • Extending Yotam's comment, there isn't actually a 'base 5' datatype. You can use `bin(x)` to convert to binary, `hex(x)` to hexadecimal and `oct(x)` to octal. But... These still all output a string. It turns out that numbers are all stored in memory as binary (Indeed, there's nothing else they could be stored as) and reporting a number as decimal is only a convention. There's nothing wrong with using strings to find base values; that's the most obvious way of outputting it. – Jakob Lovern Dec 11 '17 at 21:30
  • Actually... What I think that the OP means is 'how can I find the base _n_ representation without keeping a string of all the possible digits?' – Jakob Lovern Dec 11 '17 at 21:31
  • @JakobLovern: Possibly. But the OP also said that they wanted a result of type `int`. – Mark Dickinson Dec 11 '17 at 21:37
  • @Mark That... Literally makes no sense, though. I guess the OP could extend the `int` class with methods for each other base? – Jakob Lovern Dec 11 '17 at 21:39
  • I don't want to have a string of all possible digits, and I want the output to be an integer does that make it clear? –  Dec 11 '17 at 21:41
  • @nick: Not really. Are you _sure_ you want the output to be an integer, rather than a string representation of an integer. In the example you give, you're putting in `14` and `2` and getting out the integer one thousand, one hundred and ten, which could also be written as `0x456`, or in binary as `0b10001010110`. It's hard to imagine what your desired output of `1110` as an `int` could possibly be useful for. An output of `"1110"` (type `str`) would make a lot more sense. – Mark Dickinson Dec 11 '17 at 21:44
  • why would `str` make more sense? –  Dec 11 '17 at 21:50

1 Answers1

2

a simple implementation which can "convert" up to base 9 is as follows:

def conver(n,b):
    a = 0
    i = 0
    while n:
        n,r = divmod(n,b)
        a += 10**i * r
        i += 1

    return a

note that n,r = divmod(n,b) could be r = n % b; n //= b

Note that the number you're getting is an integer all right (as a type), but it makes no sense using it as such because the base is wrong: Ex: 1110 is not really 1110. So it's not really different than a string...

Well, that's just for the sake of the exercise.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219