Question

In: Computer Science

The hexademical number system uses base 16 with digits 0, 1, 2, 3, 4, 5, 6,...

The hexademical number system uses base 16 with digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. Hexadecimal is often used in computer systems programming. Write a Python program, in a file called convertHex.py, to request a decimal number from the user and output the hexadecimal value. To compute the hexadecimal value we have to find the hexadecimal digits hn, hn-1, hn-2, ..., h2, h1, and h0, such that

  d = hn x 16n + hn-1 x 16n-1 + hn-2 x 16n-2 + ... + h2 x 162 + h1 x 161 +  h0 x 160

These hexadecimal digits can be found by successively dividing d by 16 until the quotient is 0; the remainders are h0, h1, ..., hn-1, hn.

For example, if d=589:

  • 589/16 is 36 with a remainder of 13 - 13 in hexadecimal is 'D' - this is h0
  • 36/16 is 2 with a remainder of 4 - 4 in hexadecimal is '4' - this is h1
  • 2/16 is 0 with a remainder of 2 - 2 in hexadecimal is '2' - this is h2

So 589 in decimal is 24D in hexadecimal.

Your program should include the following functions:

  • decToHex(dec_value) - returns the hexadecimal equivalent of dec_value (as a string)
  • getHexChar(dec_digit) - returns the hexadecimal digit for dec_digit (note that 10 in decimal is 'A' in hex, 11 in decimal is 'B', etc)

Sample output:

Enter decimal value: 589
589 is equal to 24D in hexadecimal

Solutions

Expert Solution

Below is your code: -

def main():

num = int(input("Enter decimal value: "))

print(num,"is equal to",decToHex(num), "in hexadecimal")

def decToHex(dec_value):

val = str()

while dec_value > 0:

hexV=dec_value%16

dec_value=dec_value//16

val = getHexChar(hexV) + val

return val

def getHexChar(dec_digit):

if dec_digit < 10:

return str(dec_digit)

if dec_digit == 10:

return "A"

if dec_digit == 11:

return "B"

if dec_digit == 12:

return "C"

if dec_digit == 13:

return "D"

if dec_digit == 14:

return "E"

if dec_digit == 15:

return "F"

main()

Output


Related Solutions

Recall that hexadecimal numbers are constructed using the 16 digits 0, 1, 2, 3, 4, 5,...
Recall that hexadecimal numbers are constructed using the 16 digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. (a) How many strings of hexadecimal digits consist of from one through four digits? (b) How many strings of hexadecimal digits consist of from two through five digits?
Hexadecimal digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C,...
Hexadecimal digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. How many hexadecimal strings of length twelve have five A’s and five B’s? How many hexadecimal strings of length twelve have at most three E’s? How many hexadecimal strings of length twelve have exactly three A’s and at least two B’s? How many hexadecimal strings of length twelve have exactly two A’s and exactly two B’s, so that the two...
exampleInput.txt 1 2 3 0 2 3 4 0 1 3 5 0 1 2 6...
exampleInput.txt 1 2 3 0 2 3 4 0 1 3 5 0 1 2 6 1 5 6 8 2 4 6 7 3 4 5 9 10 5 8 9 4 7 9 6 7 8 6 How can I detect when 'cin' starts reading from a new line. The amount of numbers in each row is unknown. I need them in type 'int' to use the data.
x (Bins) frequency 0 0 1 0 2 0 3 2 4 5 5 8 6...
x (Bins) frequency 0 0 1 0 2 0 3 2 4 5 5 8 6 13 7 33 8 42 9 66 10 77 11 105 12 103 13 110 14 105 15 84 16 70 17 51 18 40 19 27 20 27 21 15 22 5 23 7 24 2 25 2 26 1 27 0 28 0 29 0 30 0 (7) On the Histogram worksheet, calculate all frequencies of the distribution using the table shown....
The binary number system uses just two digits (0 and 1) to represent any counting number....
The binary number system uses just two digits (0 and 1) to represent any counting number. Match each of the familiar 'base-10' numbers below with their 8-digit binary equivalents. Group of answer choices 7       [ Choose ]            00001111            01000001            00001101            00001000       8       [ Choose ]            00001111            01000001            00001101            00001000   ...
6 5 4 5 0 0 13 48 6 1 0 7 2 0 1 1...
6 5 4 5 0 0 13 48 6 1 0 7 2 0 1 1 0 2 11 5 11 27 4 0 6 Create Standard Deviation Chart (Normal Distribution Curve)
ID X Y 1 2 3 2 3 6 3 4 6 4 5 7 5...
ID X Y 1 2 3 2 3 6 3 4 6 4 5 7 5 8 7 6 5 7 7 6 7 8 8 8 9 7 8 10 12 11 Test the significance of the correlation coefficient. Then use math test scores (X) to predict physics test scores (Y).  Do the following: Create a scatterplot of X and Y. Write the regression equation and interpret the regression coefficients (i.e., intercept and slope). Predict the physics score for each....
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4 6 5 4 5 3 7 5 5 4 2 6 5 6 6] This is my dataset Find mean, median, mode, variance, standard deviation, coefficient of variation, range, 70th percentile, 3rdquartile of the data and skewness and define what each of these statistics measure. For example, mean is a measure of the central tendency, what about the rest? Use Chebyshev’s rule to find...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4 6 5 4 5 3 7 5 5 4 2 6 5 6 6] This is my dataset Split the dataset in two equal parts. You have 30 datavalues. If you split the data in two equal parts each part will contain 15 data values.  Call the first part Y and second part X.Draw scatter plot of the 2 datasets, X being on the horizontal...
How many 3-digit numbers can we make using the digits 1, 2, 3, 4, 5, 6...
How many 3-digit numbers can we make using the digits 1, 2, 3, 4, 5, 6 without repetitions? How about with repetitions (meaning different digits can hold the same number; e.g., 223 and 444 are valid 3-digit numbers in the with repetitions case)?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT