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]
#6
That is the kind of question you should answer yourself.
[pythonimport string

characters = string.digits + string.ascii_uppercase
indexes = (9, 10, 11)
a = (characters[x] for x in indexes)
b = list(a)
c = "-".join(b)
print("characters", characters)
print("a", a)
print("b", b)
print("c", c))[/python]
Output:
characters 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ a <generator object <genexpr> at 0x00000128B57E3300> b ['9', 'A', 'B'] c 9-A-B
From the output you can see:

characters is a str that contains all the characters you'll use for any number base up to 36.

(characters[x] for x in indexes) is a generator object.

The generator will return a series. We can use list() to unpack the series so we can print it out. The printed series show that generator object converts the indexes from ints to characters.

join() if is a str method. You can read about it here:

https://docs.python.org/3/library/stdtyp...l#str.join

In my example I used '-' as the separator so it is more easily seen.

Now do you know what line 16 does?
SpongeB0B likes this post
Reply


Messages In This Thread
RE: Convert an Interger into any base !? - by deanhystad - Jan-10-2023, 08:23 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,198 Feb-29-2024, 12:30 AM
Last Post: Winfried
  is there any tool to convert negative base to int? Skaperen 7 2,477 May-27-2022, 07:30 AM
Last Post: Gribouillis
  Convert list to interger Clives 5 1,681 May-09-2022, 12:53 PM
Last Post: deanhystad
Thumbs Up Convert ActiveDirectory timestamp into regular one. Arrow (solved) SpongeB0B 2 1,991 Nov-02-2020, 08:34 AM
Last Post: bowlofred
  convert non-string with explicit base jacklee26 5 16,583 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