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] Write a program to convert a hexadecimal number to its decimal value using the...

    [Using Python] Write a program to convert a hexadecimal number to its decimal value using the ord builtin function (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`...

  • [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...

  • 3.8) Convert the hexadecimal number 0x15 to a decimal number. 3.9) Convert the hexadecimal number 0x19...

    3.8) Convert the hexadecimal number 0x15 to a decimal number. 3.9) Convert the hexadecimal number 0x19 to a decimal number. 3.10) Convert the decimal number -35 to an 8-bit two’s complement binary number. 3.11) Convert the decimal number -32 to an 8-bit two’s complement binary number. 3.12) Assuming the use of the two’s complement number system find the equivalent decimal values for the following 8-bit binary numbers: a)10000001 b)11111111 c)01010000 d)11100000 e)10000011 3.13) Convert the base 8 number 204 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...

  • JAVA PLEASE 4. Write a recursive program that converts a Hex base value to its decimal...

    JAVA PLEASE 4. Write a recursive program that converts a Hex base value to its decimal equivalent. This program must validate the ueser’s input. You program must have the main method, inputValidate method and hexToDecimal method. Must implement the last method recursively. Here is a sample output. Enter a valid hex number: 10 Your number in decimal is: 16 Do you have another number: yes Enter a valid hex number: 123 Your number in decimal is: 291 Do you have...

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