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

The Archive Base Latest Questions

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

How would you recursively or iteratively change a decimal to hexadecimal? I wrote a

  • 0

How would you recursively or iteratively change a decimal to hexadecimal?

I wrote a sample program that does not really work:

def ChangeHex(n):
    if (n < 0):
        print(0)
    elif (n<=1):
        print(n)
    else:
        ChangeHex(n / 16)
        if (n == 15):
            print("F")
        if (n == 14):
            print("E")
        if (n == 13):
            print("D")
        if (n == 12):
            print("C")
        if (n == 11):
            print("B")
        if (n == 10):
            print("A")
        n % 16

How would I make it work properly? I know there is a built in function but I want to do it this way.

  • 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-21T16:13:41+00:00Added an answer on May 21, 2026 at 4:13 pm
    # Converts a decimal number to hexadecimal.
    # Executes a zero-fill for up to six digits.
    # This is used for correct conversion back
    # to the instruction format. See zero_fill().
    # @param  dec  Decimal representation of instruction
    # @return  Zero-filled hexadecimal instruction.
    def convert(dec):
    # BEGIN convert()
        hex = "%X" % dec
        return zero_fill(hex, 6)
    # END convert()
    
    # Prepends zeros until the specified
    # length is reached. Works recursively.
    # @param  n  Number to fill
    # @param  length  Length to reach
    # @return  Zero-filled number
    def zero_fill(n, length):
    # BEGIN zero_fill()
        # Check if length requirement is met
        if len(n) != length:
            # Requirement not met, run function again with
            # n having one prepended zero.
            return zero_fill('0'+n, length)
        else:
            # Requirement met, return n.
            return n
    # END zero_fill()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to run a bash script that recursively cleans up every filename
I am trying to divide arrays recursively... I think that is what this would
Would it not make sense to support a set of languages (Java, Python, Ruby,
Would the following SQL remove also the index - or does it have to
Would having a nice little feature that makes it quicker to write code like
I have a TreeView which i build up in code recursively. I would like
I'm trying to come up with a program that helps me manage my tv
(please excuse that I didn't use aliases). I would like my query output to
I would like to recursively call a servlet from itself until my operation completes.
What would be the best way to recursively scan through a directory in c++?

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.