Advertisement
FranzVuttke

hex2dec

Jan 10th, 2023
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. def hex2dec(ahex:str):
  2.     ahex = ahex[::-1].upper()
  3.     result = 0
  4.     for i in range(0, len(ahex)):
  5.         buff = ahex[i]
  6.         if not buff.isdigit():
  7.             if not buff in ["A","B","C","D","E","F"]:
  8.                 return None
  9.             buff = {"A":"10",
  10.                     "B":"11",
  11.                     "C":"12",
  12.                     "D":"13",
  13.                     "E":"14",
  14.                     "F":"15"}.get(buff,0)
  15.         result += int(buff) * 16**i
  16.  
  17.     return result
  18.  
  19. valh = "ada"
  20. print(valh, hex2dec(valh),hex2dec(valh) == int(valh, 16))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement