Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
converting int to any base
#1
i would like to know in there is a way to convert an int to a string of digit, with a specified minimum width and a specified leading character (if there is room for it) in any base > 1 for which enough digits can be provided ... or if i need to code my own function.
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
conversion, string, format.... based on that I suggest to look at f-strings
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
... forgot to mention, i need to do this on Python versions [((241806727153106%40**(p+1))//40**p)/10. for p in range(9)].
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
Quick and dirty function which does some required stuff:

def int_to_base(num, base):
    if num == 0:
        return '0'
    digits = ''
    while num:
        digits = digits + str(num % base)
        num //= base 
    return 'meaning_of_life: {0:08d}'.format(int(digits[::-1]))

int_to_base(42, 8)
'meaning_of_life: 00000052'
Unfortunately // and / have different meanings in 2 and 3.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

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