# Convert number bases [duplicate]

be as short (in bytes) as possible
take 3 inputs that are strings: number , fromdigits, todigits
assume no digit is repeated in fromdigits or todigits
assume fromdigit and todigit contain only alphanumeric characters
assume number is a natural number
output (a string): number converted to frombase
examples:
convert("ff","0123456789abcdef","0123456789")="255"
convert("ee","fecdba9876543210","9876543210")="82"
example code (283 bytes, in python 3.4.1):

def convert(number,fromdigits,todigits):
x=0
for digit in str(number):
x = x*len(fromdigits)+fromdigits.index(digit)
res=""
while x>0:
digit = x % len(todigits)
res = todigits[digit] + res
x //= len(todigits)
return res

• For a fastest code challenge, you need to say how you're going to measure the speed of the code. What inputs will be used? On what system will it be measured?
– xnor
Commented Oct 1, 2014 at 3:18
• be as short (in bytes) as possible Commented Oct 1, 2014 at 3:22
• Then the tag you're looking for is code-golf.
– xnor
Commented Oct 1, 2014 at 3:23
• Almost there! You should specify the format of the input and output. You say "program" but define a function. The default is to allow both. Also, is there a limit to the characters that may be used to represent digits?
– xnor
Commented Oct 1, 2014 at 3:26
• The output of the convert("ff","0123456789abcdef","0123456789") call should be 255 instead of 256 Commented Oct 1, 2014 at 7:26

# Python - 101

To get started I basically golfed your example code...

def c(n,f,t,x=0,r=""):
for d in n:x=x*len(f)+f.index(d)
while x:l=len(t);r=t[x%l]+r;x//=l
return r


## APL (40)

Can't beat CJam anymore it seems.

{⎕IO←0⋄⍵⍵[N⊤⍨(⌊1+Z⍟N←(⍴⍺⍺)⊥⍺⍺⍳⍵)/Z←⍴⍵⍵]}


This takes the fromdigits and todigits values as its ⍺⍺ and ⍵⍵ arguments, and the number itself as the ⍵ argument, i.e.:

      ('0123456789abcdef'{⎕IO←0⋄⍵⍵[N⊤⍨(⌊1+Z⍟N←(⍴⍺⍺)⊥⍺⍺⍳⍵)/Z←⍴⍵⍵]}'0123456789')'ff'
255
convert ← {⎕IO←0⋄⍵⍵[N⊤⍨(⌊1+Z⍟N←(⍴⍺⍺)⊥⍺⍺⍳⍵)/Z←⍴⍵⍵]}
('0123456789abcdef' convert '0123456789') 'ff'
255
('fedcba9876543210' convert '9876543210') 'ee'
82
('0123456789' convert '01') '254'
11111110


Explanation:

• ⎕IO←0: set the index origin to 0. (APL arrays start at 1 by default and we don't need that here.)
• Z←⍴⍵⍵: store the length of ⍵⍵ in Z. This is the base to convert to.
• N←(⍴⍺⍺)⊥⍺⍺⍳⍵: look up the index (⍳) in ⍺⍺ of each value in ⍵. These are the digits in the base to convert from. Then decode (⊥) this from the input base, which is the length of ⍺⍺. Store this value in N.
• ⌊1+Z⍟N: find the amount of digits required to represent value N in base Z.
• (...)/Z: replicate Z that many times.
• N⊤⍨: encode N into the output base.
• ⍵⍵[...]: for each digit of that, find the corresponding character in ⍵⍵.
• Why do you use 2 character variable names, plus, since those are unicodes, they are 4 bytes each Commented Oct 1, 2014 at 10:23
• @Optimizer: the APL charset fits in a byte just fine, and the 2-character variable names are just what APL calls them. ⍺ and ⍵ are function arguments, and ⍺⍺ and ⍵⍵ are the operator arguments. What this code is in fact doing is defining a 'convert' operator that takes two base strings on ⍺⍺ and ⍵⍵, which then gives a function that takes the number to be converted on ⍵. Commented Oct 1, 2014 at 11:13

## CJam, 20 bytes

rr:Ff#F,bAbsr:Tf#T,b


Input is like:

ee fecdba9876543210 9876543210


Output:

82


Try it online here