Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert int to other bases
#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


Messages In This Thread
convert int to other bases - by Skaperen - Sep-29-2020, 06:11 PM
RE: convert int to other bases - by bowlofred - Sep-29-2020, 06:22 PM
RE: convert int to other bases - by buran - Sep-29-2020, 06:30 PM
RE: convert int to other bases - by bowlofred - Sep-29-2020, 06:46 PM
RE: convert int to other bases - by buran - Sep-29-2020, 07:00 PM
RE: convert int to other bases - by Skaperen - Sep-30-2020, 04:08 AM
RE: convert int to other bases - by bowlofred - Sep-30-2020, 04:56 AM
RE: convert int to other bases - by Skaperen - Sep-30-2020, 10:03 PM

Forum Jump:

User Panel Messages

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