Is there a built-in function in Python to change the base of an integer?

WBOY
Release: 2024-02-05 21:18:07
forward
373 people have browsed it

Python 中是否有内置函数可以更改整数的基数?

Question content

python can use the int() function to convert an integer with base 10 into base x Integer, where 2<=x<=36. For any integer, it seems natural (unless you've studied group theory) that there is a way to convert the number back to base 10, or from base x to base y.

I could write a function to convert between bases using from math import log, but I'd like to know if there's anything already built into python to do this.

Is there a built-in function in python 3 that can perform a base change between any two bases? Are there similar constraints to int() (if any)?

Edit: To clarify, int() only interprets the first argument in base 10. I want to specify this parameter as a different radix representation. I'm wondering if there is a built-in function that requires two arguments as a base representation. Example (why int() is not the answer:

>>> int('100',9) == int(str(int('81',9)),10)
false
Copy after login

Edit #2: Suppose I want to know what the base 9 value of 100 is, I would use the following, int('100', 9) This will return the value 81. Now let's say I want to take 81 (base 9) and convert it back to base 10. Is there a built-in function in python to do this, or a way to mark the first argument to indicate that I'm giving it a base 9 number?

I know there is a way to specify a binary value using 'ob' and a hexadecimal value using '0x'. For example,

int('0b1100100', 2)
Copy after login

This will return the value 100, What is the prefix to specify another base for the first argument.


Correct Answer


When you talk about an integer object, it is different from its base n representation.

Although internally, integers are stored in binary, integer objects themselves have no base. You can construct an int object from any numeric string in any base n as long as you specify the radix and the numeric string is valid for that base. This is usually limited to a maximum of 36, since there are 10 decimal digits and 26 alphabets.

There are some special cases for bases 2, 8, 10, and 16 because they are the most commonly used. For these bases, you specify the base in the string representation itself. You can use the 0b, 0o, or 0x prefixes to represent 2, 8, and 16 respectively. Decimal is the default and most commonly used, so no prefix is ​​needed.

If you want to return the base n representation of an integer as a string, i.e. the inversion operation int(n, n), you have several options. You can use gmpy.digits(n, n) where n is your int and n is the base you want. This might be a bit overkill as it requires gmpy to be installed.

You can also use something like this:

import string

def int_to_base(n, n):
    """ return base n representation for int n. """
    base_n_digits = digits + ascii_lowercase + ascii_uppercase
    result = ""
    if n < 0:
        sign = "-"
        n = -n
    else:
        sign = ""
    while n > 0:
        q, r = divmod(n, n)
        result += base_n_digits[r]
        n = q
    if result == "":
        result = "0"
    return sign + "".join(reversed(result))
Copy after login

Using this function or similar functions, you can return a string representation of an integer n in base n, in base n, so:

>>> base_n_string = "81"
>>> base = 9
>>> int_to_base(int("81", 9), 9) == "81"
True
Copy after login

The above is the detailed content of Is there a built-in function in Python to change the base of an integer?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!