Created
September 25, 2014 18:44
-
-
Save zokis/7d6409da3f9ceb9865bf to your computer and use it in GitHub Desktop.
convert
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
def conv(number, from_digits, to_digits): | |
if len(to_digits) == 1: | |
return to_digits * number | |
x = 0 | |
len_from_digits = len(from_digits) | |
for digit in str(number): | |
x = x * len_from_digits + from_digits.index(digit) | |
if x == 0: | |
res = to_digits[0] | |
else: | |
len_to_digits = len(to_digits) | |
res = '' | |
while x > 0: | |
digit = x % len_to_digits | |
res = to_digits[digit] + res | |
x = int(x // len_to_digits) | |
return res | |
def int_to_basex(number, char_set): | |
return conv(number, '0123456789', char_set) | |
def basex_to_int(number, char_set): | |
return conv(number, char_set, '0123456789') | |
char_set = 'MARCELOfonsecaT@mB4lo' | |
print int_to_basex(666, char_set) | |
print basex_to_int('AE', char_set) | |
print int_to_basex(666, '|') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment