Python Forum
Convert an Interger into any base !? [Solved]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert an Interger into any base !? [Solved]
#5
(Jan-10-2023, 04:26 PM)bowlofred Wrote: So have it map from the remainders to the index of your characters.

import string

def numberToBase(n, b):
    if n == 0:
        return [0]
    digits = []
    while n:
        digits.append(int(n % b))
        n //= b
    return digits[::-1]

indexes = numberToBase(1984, 27)
print(indexes)

characters = string.digits + string.ascii_uppercase
output = "".join(characters[x] for x  in indexes)
print(output)
Output:
[2, 19, 13] 2JD

whats does the line 16 ?
[Image: NfRQr9R.jpg]
Reply


Messages In This Thread
RE: Convert an Interger into any base !? - by SpongeB0B - Jan-10-2023, 06:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 1,317 Feb-29-2024, 12:30 AM
Last Post: Winfried
  is there any tool to convert negative base to int? Skaperen 7 2,529 May-27-2022, 07:30 AM
Last Post: Gribouillis
  Convert list to interger Clives 5 1,700 May-09-2022, 12:53 PM
Last Post: deanhystad
Thumbs Up Convert ActiveDirectory timestamp into regular one. Arrow (solved) SpongeB0B 2 2,004 Nov-02-2020, 08:34 AM
Last Post: bowlofred
  convert non-string with explicit base jacklee26 5 16,674 Nov-06-2018, 06:50 AM
Last Post: jacklee26

Forum Jump:

User Panel Messages

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