Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert int to other bases
#1
i have a big int. i want to convert it to a string in base 36. i know of functions to do that in base 10 and base 16 (str and hex). but what about other bases like 32 or 36?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Divide by the base. The remainder is your least-significant digit. The quotient is the rest of the problem. Repeat for more significant digits until the number is zero.

def int_to_other_base(num, base):
    ans = []
    while num > 0:
        num, remainder = divmod(num, base)
        ans.append(remainder)
    return ans[::-1]


print(int_to_other_base(3600,36))
print(int_to_other_base(8,2))
Output:
[2, 28, 0] [1, 0, 0, 0]
36**2 = 1296. 2 times that is 2592. 36 * 28 is 1008. 1008 + 2592 is 3600. So in base 36, 3600 has digits of 2, 28, and 0.
Reply
#3
int() takes optional argument base (2-36) with default value of 10

link to the docs
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
This one creates strings suitable for putting back into int(x, base=36). Not as useful for arbitrarily large bases.

import string
import random

base = 36
mapping = string.digits + string.ascii_uppercase

def int_to_other_base(num, base):
    ans = ""
    while num > 0:
        num, remainder = divmod(num, base)
        ans = mapping[remainder] + ans
    return ans

num = random.randint(0,1000000000000000)

print(f"For integer {num}")
rebased = int_to_other_base(num,base)
print(f"In base {base}: {rebased}")
print(f"and converted back to an int: {int(rebased,base)}")
Output:
For integer 19520920808903 In base 36: 6X3SC8FRB and converted back to an int: 19520920808903
Reply
#5
I misinterpreted the question, but @bowlofred cleared my confusion
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
i already have a generic implementation that handles any base and varied sequences of digits. it gets the base from the len() of the digits string. i was just looking for a distributed implementation.

it only handles base > 1. i've been thinking of adding negative base support.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
Baseconvert is an available Pypi module.
Reply
#8
if you want a number limited to numerics and case insensitive alpha for its digits then the maximum base is 36. with case sensitive alpha that goes up to 62. that's why the various base 64 conversions and encodings have 2 more characters added. but if you want to allow anything then your limit is whatever set of digits you can use, potentially as high as 1114112. you can go even higher by creating a list of ints as the resulting sequence of digits.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

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