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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 522k
  • Answers 522k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No, there is none: you can catch it with a… May 16, 2026 at 9:26 pm
  • Editorial Team
    Editorial Team added an answer You can create an ImageIcon from a URL, among other… May 16, 2026 at 9:26 pm
  • Editorial Team
    Editorial Team added an answer Your title field is not indexed. Change new Field("title", "mydoc",… May 16, 2026 at 9:26 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

easy_install python extension allows to install python eggs from console like: easy_install py2app But
I am building an application plugin in Python which allows users to arbitrarily extend
The Python list comprehension syntax makes it easy to filter values within a comprehension.
I want to build an executable to distribute to people without python installed on
Is there a GUI for IPython that allows me to open/run/edit Python files? My
In the python program I'm writing, I've got a thread which iterates over a
I would like to write a Skype like software which allows P2P video/audio streaming.
I'm currently attempting to migrate a legacy VBA/Microsoft Access application to Python and PyQt.
I'm running Python 2.6.1 on Windows XP SP3. My IDE is PyCharm 1.0-Beta 2
I'm trying to run an Icecast stream using a simple Python script to pick

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.