Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 597701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:18:32+00:00 2026-05-13T16:18:32+00:00

Python allows easy creation of an integer from a string of a given base

  • 0

Python allows easy creation of an integer from a string of a given base via

int(str, base). 

I want to perform the inverse: creation of a string from an integer,
i.e. I want some function int2base(num, base), such that:

int(int2base(x, b), b) == x

The function name/argument order is unimportant.

For any number x and base b that int() will accept.

This is an easy function to write: in fact it’s easier than describing it in this question. However, I feel like I must be missing something.

I know about the functions bin, oct, hex, but I cannot use them for a few reasons:

  • Those functions are not available on older versions of Python, with which I need compatibility with (2.2)

  • I want a general solution that can be called the same way for different bases

  • I want to allow bases other than 2, 8, 16

Related

  • Python elegant inverse function of int(string, base)
  • Integer to base-x system using recursion in python
  • Base 62 conversion in Python
  • How to convert an integer to the shortest url-safe string in Python?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:18:32+00:00Added an answer on May 13, 2026 at 4:18 pm

    If you need compatibility with ancient versions of Python, you can either use gmpy (which does include a fast, completely general int-to-string conversion function, and can be built for such ancient versions – you may need to try older releases since the recent ones have not been tested for venerable Python and GMP releases, only somewhat recent ones), or, for less speed but more convenience, use Python code – e.g., for Python 2, most simply:

    import string
    digs = string.digits + string.ascii_letters
    
    
    def int2base(x, base):
        if x < 0:
            sign = -1
        elif x == 0:
            return digs[0]
        else:
            sign = 1
    
        x *= sign
        digits = []
    
        while x:
            digits.append(digs[int(x % base)])
            x = int(x / base)
    
        if sign < 0:
            digits.append('-')
    
        digits.reverse()
    
        return ''.join(digits)
    

    For Python 3, int(x / base) leads to incorrect results, and must be changed to x // base:

    import string
    digs = string.digits + string.ascii_letters
    
    
    def int2base(x, base):
        if x < 0:
            sign = -1
        elif x == 0:
            return digs[0]
        else:
            sign = 1
    
        x *= sign
        digits = []
    
        while x:
            digits.append(digs[x % base])
            x = x // base
    
        if sign < 0:
            digits.append('-')
    
        digits.reverse()
    
        return ''.join(digits)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The yield statement in python allows simple iteration from a procedure, and it also
I'm using python, and I want a function that takes a string containing a
Is there some module to allow for easy DB provider configuration via connection string,
easy_install python extension allows to install python eggs from console like: easy_install py2app But
I'm looking for a python web framework that is easy to use and allows
Python allows aliasing of imports, through ...as <ALIAS> clauses in the import statement, like
Decorative pattern in Python allows to wrap the core object with other object capable
I am curretnly attempting to write a script in python that allows me to
hi im looking for some modules for python 2.5 whitch allows to run and
As part of teaching myself python I've written a script which allows a user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.