0

I'm trying to figure out an efficient way (without recursion) to convert a string (number representation) from one arbitrary base to another using custom alphabet. But I cannot think of a very fast efficient method (byte shift?) to do it:

def convert_base(target, base_from, base_to):
  alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  base_to_alphabet_slice = alphabet[:base_to]
  base_from_alphabet_slice = alphabet[:base_from]
  result = ''
  for char in target:
    result += ...
  return result

Expected results:

print(convert_base('E', 16, 2))

Output: '1110'

print(convert_base('1110', 2, 16))

Output: 'E'

print(convert_base('E', 16, 10))

Output: '14'

Can someone suggest a good method for this?

0 Answers0