Skip to content

Instantly share code, notes, and snippets.

@Tony3-sec
Created October 3, 2015 17:11
Show Gist options
  • Save Tony3-sec/8fdb7f69d02582712cef to your computer and use it in GitHub Desktop.
Save Tony3-sec/8fdb7f69d02582712cef to your computer and use it in GitHub Desktop.
#!/usr/bin/python
## Converts hex to decimal
# Dictionary for hexadecimal. Covers both lower case and upper case.
hexDict = {'a':10, 'b':11, 'c':12, 'd':13, 'e':14, 'f':15, 'A':10, 'B':11, 'C':12, 'D':13, 'E':14, 'F':15}
result = 0
# Get user input
myhex = raw_input('Enter hex: ')
# Reverse the input
myhex = myhex[::-1]
for i,j in enumerate(myhex):
# If the input contains "a-f(A-F)", convert to digits
if hexDict.get(j): # Use get() to avoid KeyError
digit = hexDict[j]
ans = int(digit) * (16 ** i)
else:
ans = int(j) * (16 ** i)
result += ans
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment