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]
#1
Thumbs Up 
Hi everyone,

Converting a ~base number into a Integer (base10) is quite straightfoward ->
int('2JD',27)
Output:
1984
But how easily do the opposite ? 1984 turned into '2JD' ?

Thanks.
[Image: NfRQr9R.jpg]
Reply
#2
Meanwhile I've found this

def numberToBase(n, b):
    if n == 0:
        return [0]
    digits = []
    while n:
        digits.append(int(n % b))
        n //= b
    return digits[::-1]
It's not returning 1984 but all the remainders : 2, 19, 13

so if I found nothing else I could modify it to return 2JD

Thanks.
[Image: NfRQr9R.jpg]
Reply
#3
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
SpongeB0B likes this post
Reply
#4
Gluing together
import string

digits = string.digits + string.ascii_uppercase

def int2base(value, base):
    if value == 0:
        return '0'
    sign = ['-'] if value < 0 else ['']
    value = abs(value)
    positions = []
    while value:
        value, rem = divmod(value, base)
        positions.insert(0, digits[rem])
    return ''.join(sign + positions)

print(int2base(-1984, 27))
Output:
-2JD
SpongeB0B likes this post
Reply
#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
#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
#7
Can also use numpy.base_repr
>>> import numpy as np
>>> 
>>> n = 1984
>>> np.base_repr(n, base=27)
'2JD'
Skaperen likes this post
Reply
#8
Module baseconvert looks interesting.
SpongeB0B likes this post
Reply
#9
Thank you all for your inputs !

As I'm working only with one base I've write myself a function (thx to @deanhystad )

and if in the future I have to work with more than one base, I think I will work with baseconvert (Thanks @Gribouillis) that seem very easy and powerful ! Too bad it's labeled as pre-release. I hope someone will fork it and maintain it.

Cheers.
[Image: NfRQr9R.jpg]
Reply


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