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

Ask A Question

Stats

  • Questions 516k
  • Answers 516k
  • 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 As you have inferred, MyClass is being passed in as… May 16, 2026 at 6:41 pm
  • Editorial Team
    Editorial Team added an answer UPDATE SubmissionVersion sv SET sv.IsFavorite = ".$Index." WHERE sv.ID =… May 16, 2026 at 6:41 pm
  • Editorial Team
    Editorial Team added an answer You don't, the AJAX query (getJSON) is asynchronous meaning that… May 16, 2026 at 6:41 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

Python allows easy creation of an integer from a string of a given base
The yield statement in python allows simple iteration from a procedure, and it also
We're using a third-party middleware product that allows us to write code in an
I am used to that Python allows some neat tricks to delegate functionality to
youtube-dl is a Python script that allows one to download YouTube videos. It supports
Is there a GUI for IPython that allows me to open/run/edit Python files? My
I'm trying to run an Icecast stream using a simple Python script to pick
In short: I want to monitor selected calls from an application to a DLL.
Rather than use an ORM, I am considering the following approach in Python and
I have a python program that has no windows frame and doesn't show up

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.