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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:29:18+00:00 2026-05-17T18:29:18+00:00

I’m trying to make a program to convert a number in any base to

  • 0

I’m trying to make a program to convert a number in any base to another base of the user’s choice. The code I have so far goes like this:

innitvar = float(raw_input("Please enter a number: "))
basevar = int(raw_input("Please enter the base that your number is in: "))
convertvar = int(raw_input("Please enter the base that you would like to convert to: "))

These are the data that I get from the user. The initial number, its initial base, and the base the user wants to convert to. As I understand it, I need to convert to base 10, and then to the desired base, specified by the user.

This is where I’m hitting a brick wall: I need to multiply the leftmost digit in the initial number by its initial base, and then add the next digit to the right, and then repeat until I hit the rightmost digit. I understand how to do this on paper, but I have no idea how to put it into Python code. I’m not sure how I would multiply the first number, and then add the next, nor do I understand how to let the program know when to stop performing this operation.

I’m not asking to have the program written for me, but I would like to be pointed in the right direction.

Thanks for your time!

  • 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-17T18:29:18+00:00Added an answer on May 17, 2026 at 6:29 pm

    This should be the first half of the answer to your problem. Can you figure out how to convert to a base?

    # Create a symbol-to-value table.
    SY2VA = {'0': 0,
             '1': 1,
             '2': 2,
             '3': 3,
             '4': 4,
             '5': 5,
             '6': 6,
             '7': 7,
             '8': 8,
             '9': 9,
             'A': 10,
             'B': 11,
             'C': 12,
             'D': 13,
             'E': 14,
             'F': 15,
             'G': 16,
             'H': 17,
             'I': 18,
             'J': 19,
             'K': 20,
             'L': 21,
             'M': 22,
             'N': 23,
             'O': 24,
             'P': 25,
             'Q': 26,
             'R': 27,
             'S': 28,
             'T': 29,
             'U': 30,
             'V': 31,
             'W': 32,
             'X': 33,
             'Y': 34,
             'Z': 35,
             'a': 36,
             'b': 37,
             'c': 38,
             'd': 39,
             'e': 40,
             'f': 41,
             'g': 42,
             'h': 43,
             'i': 44,
             'j': 45,
             'k': 46,
             'l': 47,
             'm': 48,
             'n': 49,
             'o': 50,
             'p': 51,
             'q': 52,
             'r': 53,
             's': 54,
             't': 55,
             'u': 56,
             'v': 57,
             'w': 58,
             'x': 59,
             'y': 60,
             'z': 61,
             '!': 62,
             '"': 63,
             '#': 64,
             '$': 65,
             '%': 66,
             '&': 67,
             "'": 68,
             '(': 69,
             ')': 70,
             '*': 71,
             '+': 72,
             ',': 73,
             '-': 74,
             '.': 75,
             '/': 76,
             ':': 77,
             ';': 78,
             '<': 79,
             '=': 80,
             '>': 81,
             '?': 82,
             '@': 83,
             '[': 84,
             '\\': 85,
             ']': 86,
             '^': 87,
             '_': 88,
             '`': 89,
             '{': 90,
             '|': 91,
             '}': 92,
             '~': 93}
    
    # Take a string and base to convert to.
    # Allocate space to store your number.
    # For each character in your string:
    #     Ensure character is in your table.
    #     Find the value of your character.
    #     Ensure value is within your base.
    #     Self-multiply your number with the base.
    #     Self-add your number with the digit's value.
    # Return the number.
    
    def str2int(string, base):
        integer = 0
        for character in string:
            assert character in SY2VA, 'Found unknown character!'
            value = SY2VA[character]
            assert value < base, 'Found digit outside base!'
            integer *= base
            integer += value
        return integer
    

    Here is the second half of the solution. By using these two functions, converting bases is very easy to do.

    # Create a value-to-symbol table.
    VA2SY = dict(map(reversed, SY2VA.items()))
    
    # Take a integer and base to convert to.
    # Create an array to store the digits in.
    # While the integer is not zero:
    #     Divide the integer by the base to:
    #         (1) Find the "last" digit in your number (value).
    #         (2) Store remaining number not "chopped" (integer).
    #     Save the digit in your storage array.
    # Return your joined digits after putting them in the right order.
    
    def int2str(integer, base):
        array = []
        while integer:
            integer, value = divmod(integer, base)
            array.append(VA2SY[value])
        return ''.join(reversed(array))
    

    After putting it all together, you should end up with the program below. Please take time to figure it out!

    innitvar = raw_input("Please enter a number: ")
    basevar = int(raw_input("Please enter the base that your number is in: "))
    convertvar = int(raw_input("Please enter the base that you would like to convert to: "))
    
    # Create a symbol-to-value table.
    SY2VA = {'0': 0,
             '1': 1,
             '2': 2,
             '3': 3,
             '4': 4,
             '5': 5,
             '6': 6,
             '7': 7,
             '8': 8,
             '9': 9,
             'A': 10,
             'B': 11,
             'C': 12,
             'D': 13,
             'E': 14,
             'F': 15,
             'G': 16,
             'H': 17,
             'I': 18,
             'J': 19,
             'K': 20,
             'L': 21,
             'M': 22,
             'N': 23,
             'O': 24,
             'P': 25,
             'Q': 26,
             'R': 27,
             'S': 28,
             'T': 29,
             'U': 30,
             'V': 31,
             'W': 32,
             'X': 33,
             'Y': 34,
             'Z': 35,
             'a': 36,
             'b': 37,
             'c': 38,
             'd': 39,
             'e': 40,
             'f': 41,
             'g': 42,
             'h': 43,
             'i': 44,
             'j': 45,
             'k': 46,
             'l': 47,
             'm': 48,
             'n': 49,
             'o': 50,
             'p': 51,
             'q': 52,
             'r': 53,
             's': 54,
             't': 55,
             'u': 56,
             'v': 57,
             'w': 58,
             'x': 59,
             'y': 60,
             'z': 61,
             '!': 62,
             '"': 63,
             '#': 64,
             '$': 65,
             '%': 66,
             '&': 67,
             "'": 68,
             '(': 69,
             ')': 70,
             '*': 71,
             '+': 72,
             ',': 73,
             '-': 74,
             '.': 75,
             '/': 76,
             ':': 77,
             ';': 78,
             '<': 79,
             '=': 80,
             '>': 81,
             '?': 82,
             '@': 83,
             '[': 84,
             '\\': 85,
             ']': 86,
             '^': 87,
             '_': 88,
             '`': 89,
             '{': 90,
             '|': 91,
             '}': 92,
             '~': 93}
    
    # Take a string and base to convert to.
    # Allocate space to store your number.
    # For each character in your string:
    #     Ensure character is in your table.
    #     Find the value of your character.
    #     Ensure value is within your base.
    #     Self-multiply your number with the base.
    #     Self-add your number with the digit's value.
    # Return the number.
    
    integer = 0
    for character in innitvar:
        assert character in SY2VA, 'Found unknown character!'
        value = SY2VA[character]
        assert value < basevar, 'Found digit outside base!'
        integer *= basevar
        integer += value
    
    # Create a value-to-symbol table.
    VA2SY = dict(map(reversed, SY2VA.items()))
    
    # Take a integer and base to convert to.
    # Create an array to store the digits in.
    # While the integer is not zero:
    #     Divide the integer by the base to:
    #         (1) Find the "last" digit in your number (value).
    #         (2) Store remaining number not "chopped" (integer).
    #     Save the digit in your storage array.
    # Return your joined digits after putting them in the right order.
    
    array = []
    while integer:
        integer, value = divmod(integer, convertvar)
        array.append(VA2SY[value])
    answer = ''.join(reversed(array))
    
    # Display the results of the calculations.
    print answer
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,

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.