The powers that be want to be able to quickly convert any number they have into their own number base using any format they would like.
Input
Your program must accept 3 parameters.
- Number: The string number to be converted
- InputFormat: the base string the number is currently in
- OutputFormat: the base string that the number is to be converted to.
Output
Your program must convert the Number
from the old number base InputFormat
to the new number base OutputFormat
Examples
("1","0123456789","9876543210") = "8"
("985724","9876543210","0123456789ABCDEF") = "37C3"
("FF","0123456789ABCDEF","0123456789") = "255"
("FF","0123456789ABCDEF","01234567") = "377"
("18457184548971248772157", "0123456789","Aa0Bb1Cc2Dd3Ee4Ff5Gg6Hh7Ii8Jj9Kk,Ll.Mm[Nn]Oo@Pp#Qq}Rr{Ss-Tt+Uu=Vv_Ww!Xx%Yy*Zz") = ",sekYFg_fdXb"
Additional
The new base 77 test is not required props if it works though
- if your in a language where you have to convert to a number first and are locked within 32Bit you can skip it.
- as it's an additional test.
All examples were generated by PHP 7.2 with the bcmath extension using the following code (vars mins but code formatted). there will probably be a shorter way this is just the way I came up with for the system I needed to do this with would be nice to see if anyone could come up with a shorter version though.
PHP 7.2 (bcmath - extension) 614 bytes
<?php
function f($a, $b, $c)
{
$d= str_split($b,1);
$e= str_split($c,1);
$f= str_split($a,1);
$g=strlen($b);
$h=strlen($c);
$k=strlen($a);
$r='';
if ($c== '0123456789')
{
$r=0;
for ($i = 1;$i <= $k; $i++)
$retval = bcadd($retval, bcmul(array_search($f[$i-1], $d),bcpow($g,$k-$i)));
return $r;
}
if ($b!= '0123456789')
$l=f($a, $b, '0123456789');
else
$l= $a;
if ($l<strlen($c))
return $e[$l];
while($l!= '0')
{
$r= $e[bcmod($l,$h)].$r;
$l= bcdiv($l,$h,0);
}
return $r;
}
Scoring
This is code golf; shortest code wins. Standard loopholes apply.
["zX", "tXdsyqzSDRP02", "brFNC02bc"] => "cb"
. (or whatever that should actually be, if that's incorrect) \$\endgroup\$