Created
September 15, 2017 16:32
-
-
Save rpromyshlennikov/81062dc6901692d74f859c017c8706bc to your computer and use it in GitHub Desktop.
Convert hex, oct stings to integer, because there is no base for int() for Python1.5.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HEX_CHARS = { | |
'0': 0, | |
'1': 1, | |
'2': 2, | |
'3': 3, | |
'4': 4, | |
'5': 5, | |
'6': 6, | |
'7': 7, | |
'8': 8, | |
'9': 9, | |
'a': 10, | |
'b': 11, | |
'c': 12, | |
'd': 13, | |
'e': 14, | |
'f': 15, | |
'A': 10, | |
'B': 11, | |
'C': 12, | |
'D': 13, | |
'E': 14, | |
'F': 15, | |
} | |
def atoi(inp): | |
if inp.startswith('0x'): | |
inp = inp[2:] | |
base = 16 | |
elif inp.startswith('0'): | |
inp = inp[1:] | |
base = 8 | |
else: | |
raise ValueError('Input string should start from "0x" for hex ' | |
'or from "0" for octal value.') | |
res = 0 | |
for index in xrange(len(inp)): | |
res = res + HEX_CHARS[inp[len(inp) - index - 1]] * base ** index | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment