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 555181
In Process

The Archive Base Latest Questions

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

Python allows conversions from string to integer using any base in the range [2,36]

  • 0

Python allows conversions from string to integer using any base in the range [2,36] using:

int(string,base)

I am looking for an elegant inverse function that takes an integer and a base and returns a string.

For example:

>>> str_base(224,15)
'ee'

I came up with the following solution:

def digit_to_char(digit):
    if digit < 10: return chr(ord('0') + digit)
    else: return chr(ord('a') + digit - 10)

def str_base(number,base):
    if number < 0:
        return '-' + str_base(-number,base)
    else:
        (d,m) = divmod(number,base)
        if d:
            return str_base(d,base) + digit_to_char(m)
        else:
            return digit_to_char(m)

Note: digit_to_char() works for bases <= 169 arbitrarily using ASCII characters after z as digits for bases above 36.

Is there a Python built‑in, library function, or a more elegant inverse function of int(string,base)?

  • 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-13T11:47:18+00:00Added an answer on May 13, 2026 at 11:47 am

    This thread has some example implementations.

    Actually I think your solution looks rather nice, it’s even recursive which is somehow pleasing here.

    I’d still simplify it to remove the else, but that’s probably a personal style thing. I think if foo: return is very clear, and doesn’t need an else after it to make it clear it’s a separate branch.

    def digit_to_char(digit):
        if digit < 10:
            return str(digit)
        return chr(ord('a') + digit - 10)
    
    def str_base(number,base):
        if number < 0:
            return '-' + str_base(-number, base)
        (d, m) = divmod(number, base)
        if d > 0:
            return str_base(d, base) + digit_to_char(m)
        return digit_to_char(m)
    

    I simplified the 0-9 case in digit_to_char(), I think str() is clearer than the chr(ord()) construct. To maximize the symmetry with the >= 10 case an ord() could be factored out, but I didn’t bother since it would add a line and brevity felt better. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python allows easy creation of an integer from a string of a given base
I have the following python function that allows me to run shell commands from
The yield statement in python allows simple iteration from a procedure, and it also
hi im looking for some modules for python 2.5 whitch allows to run and
As of 2.4 (2.6 for classes), python allows you to decorate a function with
I wrote a simple webapp using google app engine in python that allows users
Python's setdefault allows you to get a value from a dictionary, but if the
Is there a library for C# that allows similar functionality to python's struct from
I am looking for a code in python that allows me to take a
I'm trying to port an application from C# to Python. The application allows the

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.