Python Forum
Convert an Interger into any base !? [Solved] - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Convert an Interger into any base !? [Solved] (/thread-39151.html)



Convert an Interger into any base !? [Solved] - SpongeB0B - Jan-10-2023

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.


RE: Convert an Interger into any base !? - SpongeB0B - Jan-10-2023

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.


RE: Convert an Interger into any base !? - bowlofred - Jan-10-2023

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



RE: Convert an Interger into any base !? - deanhystad - Jan-10-2023

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



RE: Convert an Interger into any base !? - SpongeB0B - Jan-10-2023

(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 ?


RE: Convert an Interger into any base !? - deanhystad - Jan-10-2023

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/stdtypes.html#str.join

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

Now do you know what line 16 does?


RE: Convert an Interger into any base !? - snippsat - Jan-10-2023

Can also use numpy.base_repr
>>> import numpy as np
>>> 
>>> n = 1984
>>> np.base_repr(n, base=27)
'2JD'



RE: Convert an Interger into any base !? - Gribouillis - Jan-11-2023

Module baseconvert looks interesting.


RE: Convert an Interger into any base !? - SpongeB0B - Jan-16-2023

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.