I am trying to add two hexadecimal numbers.
def addbinary(n1,n2):
s = ""
carry = '0'
for i in range(3,-1,-1):
if (n1[i] == '1' and n2[i] == '1' and carry == '0'):
s = '0' + s
carry = '1'
elif (n1[i] == '1' and n2[i] == '1' and carry == '1'):
s = '1' + s
carry = '1'
elif (n1[i] == '1' and n2[i] == '0' and carry == '0'):
s = '1' + s
carry = '0'
elif (n1[i] == '1' and n2[i] == '0' and carry == '1'):
s = '0' + s
carry = '1'
elif (n1[i] == '0' and n2[i] == '1' and carry == '0'):
s = '1' + s
elif (n1[i] == '0' and n2[i] == '1' and carry == '1'):
s = '0' + s
elif (n1[i] == '0' and n2[i] == '0' and carry == '1'):
s = '1' + s
carry = '0'
elif (n1[i] == '0' and n2[i] == '0' and carry == '0'):
s = '0' + s
if carry == '1':
s = '1' + s
return s
def addhex(n1):
hextable = {
'0' : 0000,
'1' : 0001,
'2' : 0010,
'3' : 0011,
'4' : 0100,
'5' : 0101,
'6' : 0110,
'7' : 0111,
'8' : 1000,
'9' : 1001,
'a': 1010,
'b': 1011,
'c' : 1100,
'd' : 1101,
'e' : 1110,
'f' : 1111
}
def main():
"""
main function
"""
userhex = raw_input("Enter a hexadecimal number: ")
userhex2 = raw_input("Enter a second hexadecimal number: ")
print addbinary(addhex(userhex.lower), addhex(userhex2.lower))
if __name__ == "__main__":
main()