fork(1) download
  1. def base (val ,b):
  2. ans = ""
  3. while int(val):
  4. ans += convert[int(val % b)]
  5. val /= b
  6. return ans
  7. convert = {
  8. 0 : "0", 1 : "1", 2 : "2", 3 : "3",
  9. 4 : "4", 5 : "5", 6 : "6", 7 : "7",
  10. 8 : "8", 9 : "9", 10 : "A", 11 : "B",
  11. 12 : "C", 13 : "D", 14 : "E", 15 : "F"
  12. }
  13. while True:
  14. val = input("Enter a number : ")
  15. if val == "D" : break
  16. val = int(val)
  17. if not val :
  18. print("Decimal : 0")
  19. print("Hexadecimal : 0")
  20. print("Decimal : %s" %(base(val,2)[::-1]))
  21. print("Hexadecimal : %s" %(base(val,16)[::-1]))
Success #stdin #stdout 0.02s 9564KB
stdin
100
300
0
D
stdout
Enter a number : Decimal : 1100100
Hexadecimal : 64
Enter a number : Decimal : 100101100
Hexadecimal : 12C
Enter a number : Decimal : 0
Hexadecimal : 0
Decimal : 
Hexadecimal : 
Enter a number :