Question

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

`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

Ans:

# The required program using builtin ordered function is as follows:

def hexToDec(hexi):
result = 0;

for ch in hexi:
if 'A' <= ch <= 'F' or 'a' <= ch <= 'f':
if 'a' <= ch <= 'f':
result = result * 16 + ord(ch) - ord('a') + 10
else:
result = result * 16 + ord(ch) - ord('A') + 10
  
elif 'G' <= ch <= 'Z' or 'g' <= ch <= 'z':
return "Incorrect hex number"
  
else:
result = result * 16 + ord(ch) - ord('0')

return "The decimal value for hex number {} is {}".format(hexi,result)
  
print(hexToDec(input("Enter a hex number: ")))
print(hexToDec(input("Enter a hex number: ")))
print(hexToDec(input("Enter a hex number: ")))
print(hexToDec(input("Enter a hex number: ")))

# Desired OUTPUT is as follows:

# PLEASE DO LIKE AND UPVOTE IF THIS WAS HELPFUL!

# THANK YOU SO MUCH IN ADVANCE!

Add a comment
Know the answer?
Add Answer to:
[Using Python] Write a program to convert a hexadecimal number to its decimal value using the...
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. (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...

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

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

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

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

  • Question (15 pts.) Convert the decimal numbers from 0 to 32 to BCD, Hexadecimal, Octal, and...

    Question (15 pts.) Convert the decimal numbers from 0 to 32 to BCD, Hexadecimal, Octal, and Binary, using the minimum number of bits or digits, and without using calculators Fill your results in the table below. BCD Hex Oct |

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

  • 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