Write a program in the language of your choice which takes as input a string rep
ID: 3795802 • Letter: W
Question
Write a program in the language of your choice which takes as input a string representation of an unsigned hexadecimal number in 32 bits and returns the positive integer that is the base 10 equivalent. You do not need to worry about negative values for this question You can use a dictionary, or brute force to convert hex digits to numbers (i.e. write a function with a bunch of if statements or a select statement to deal with digits A-F. or store the values 0-F as keys in a dictionary with the values being the number the digit represents.
Explanation / Answer
Code in Python
map = {}
map['0'] = '0000'
map['1'] = '0001'
map['2'] = '0010'
map['3'] = '0011'
map['4'] = '0100'
map['5'] = '0101'
map['6'] = '0110'
map['7'] = '0111'
map['8'] = '1000'
map['9'] = '1001'
map['A'] = '1010'
map['B'] = '1011'
map['C'] = '1100'
map['D'] = '1101'
map['E'] = '1110'
map['F'] = '1111'
def hexToBin(hexStr):
binStr = ''
for hexChar in hexStr:
binStr += map[hexChar]
return binStr
def binToDec(binStr):
decValue = 0
for binChar in binStr:
decValue = decValue * 2 + int(binChar)
return decValue
hexStr = raw_input("Enter unsigned Hexadecimal number")
binaryStr = hexToBin(hexStr)
decValue = binToDec(binaryStr)
print decValue
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.