Question

[Using Python] Write a program to convert a hexadecimal number to its decimal value. (Reminder: hexadecimal...

[Using Python]

Write a program to convert a hexadecimal number to its decimal value.

(Reminder: hexadecimal numbers are 0 through 9, A,B,C,D,E,F. hex(A) = 10, hex(F) = 15).

example outputs:

1. `Enter a hex number: f`

`The decimal value for hex number f is 15`

2. `Enter a hex number: g`

`Incorrect hex number`

3. `Enter a hex number: 091c`

`The decimal value for hex number 091c is 2332`

4. `Enter a hex number: 091g`

`Incorrect hex number`

Hints:

  • you need to use the ord builtin function
  • you need to iterate through the input hex number using a loop, referencing the index of each hex character
  • you need to convert the input hex number to upper case
  • Make sure there is a invalid response for invalid input.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

# function to convert the hexadecimal number to its decimal equivalent

def hex_to_decimal(hex_num):

    # dictonary to tell the value of hex num

    hex = {'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}

    # creating list of chars and reversing it

    hex_num = hex_num.upper()

    chars = list(hex_num)

    chars.reverse()

    decimal = 0

    # looping through each char and adding to result

    for i in range(len(chars)):

        # if invalid input

        if chars[i] not in hex:

            return -1

        else:

            decimal += hex[chars[i]] * (16 ** i)

            print()

    

    # return the decimal value

    return decimal

# driver code

if __name__ == "__main__":

    hex_num = input('Enter a hex number: ')

    if hex_to_decimal(hex_num) == -1:

        print('Incorrect hex number')

    else:

        print('The decimal value for hex number {} is {}'.format(hex_num, hex_to_decimal(hex_num)))

Enter a hex number: f The decimal value for hex number f is 15

FOR HELP PLEASE COMMENT.
THANK YOU

Add a comment
Know the answer?
Add Answer to:
[Using Python] Write a program to convert a hexadecimal number to its decimal value. (Reminder: hexadecimal...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • [Using Python] I need my code given below to loop the user input, so that you...

    [Using Python] I need my code given below to loop the user input, so that you are asked to input without it stopping after 4 inputs. Code: #A program that converts a hexadecimal number to its decimal value #Define function for hexadecimal to decimal def hexToDec(hexi): result = 0 #For loop to test input for correct values for char in hexi: if 'A' <= char <= 'F' or 'a' <= char <= 'f': if 'a' <= char <= 'f': result...

  • In Python, revise this code. define convert(s): """ Takes a hex string as input. Returns decimal...

    In Python, revise this code. define convert(s): """ Takes a hex string as input. Returns decimal equivalent. """ total = 0 for c in s total = total * 16 ascii = ord(c if ord('0) <= ascii <= ord('9'): #It's a decimal number, and return it as decimal: total = total+ascii - ord('0') elif ord('A") <= ascii <= ord('F'): #It's a hex number between 10 and 15, convert and return: total = total + ascii - ord('A') + 10 else...

  • C++ program to convert between decimal, hexadecimal, and octal. Please Help!!

    Hi, I need help writing a program that reads in data (hex, octal or decimal values) from an input file and outputs the values in to another base form (hex, octal,decimal) one line at a time depending on the formatting characters provided by the input file. I am posting the code requirements below and an example of what theinput file will look like and what should be output by the program. I only need the one .cpp program file. Thanks...

  • Java: can also use “if else” statements Write a program that can convert an integer between...

    Java: can also use “if else” statements Write a program that can convert an integer between 0 and 15 into hex number (including 0 and 15). The user enters an integer from the console and the program displays the corresponding hex number. If the user enters an integer out of range, the program displays a warning message about the invalid input. Table. Conversion between Decimal and Hexadecimal Decimal Hexadecimal 0 0 1 1 2 2 3 3 4 4 5...

  • Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out...

    Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out the sum of the two numbers in hexadecimal. (As noted in class, first do this without using a file and by reading using the cin > > command) From Wikipedia: "In mathematics and computer science, hexadecimal (also base 16, or hex) is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0-9 to...

  • Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

    Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...

  • The code is to convert the fractional part of a decimal number to its hexadecimal form....

    The code is to convert the fractional part of a decimal number to its hexadecimal form. Can you explain the code, for example ,why the fractional part need to multiply 16 instead of devise 16? FracNum DecNum-num int count, IntPart float FracPart; float temp temp-FracNum *16; FracPartstemp- (int) temp; //getting Fractional part from temp IntPart (int) temp; count-e ise while (FracPart >e && count<=6) //while fractional part e and count Less than or equal to 6 then //Now taking fractional...

  • Please help me with the following C Programming project. I am providing the code I used...

    Please help me with the following C Programming project. I am providing the code I used which needs to be reworked to add the following requirements. Here is my code #include<stdio.h> char input; int main() { int i = 0; while (true){ scanf_s("%c", &input); if (input == 'X') break; printf("%c", input); } return 0; } Here are the requirements for the code plus and additional note what the code should have. Goals Understand the ASCII representation of character values. Understand...

  • USING PYTHON PLEASE Write a program that calculates the factorial value of a number entered by...

    USING PYTHON PLEASE Write a program that calculates the factorial value of a number entered by the user. Remember that x! =x* (x-1)* (x-2)*... *3+ 2* 1. Your program should check the value input by the user to ensure it is valid (i.e., that it is a number > =1). To do this, consider looking at the is digit() function available in Python. If the user enters an incorrect input, your program should continue to ask them to enter a...

  • Requriements: Submit only the files requested Print all floats to 2 decimal points unless stated otherwise...

    Requriements: Submit only the files requested Print all floats to 2 decimal points unless stated otherwise Descripition : For this problem you will be implementing a Caesarin Cipher. A Caesarin Cipher takes a string and a shift amount and shift the characters in the string by the shift amount. Specifications: Only characters should be ciphered If a character is shifted beyond the end of the aphabet it should wrap back around For example if the letter 'y' is shifted 3...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT