30
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.
How are bigger digits i.e 50,73 denoted? – Windmill Cookies – 2018-08-16T15:40:52.990
5@WindmillCookies By whatever characters are in the format strings. – Adám – 2018-08-16T15:43:07.670
1Oh wait. I had understood the question completely wrong, now understood correctly, thanks! – Windmill Cookies – 2018-08-16T15:43:58.090
6Nice first question! :-) – Giuseppe – 2018-08-16T15:49:23.480
@Giuseppe Thanks believe it or not the reason for this is that have just had to do something that involved converting to a custom number base (40) for work, and thought this would be an awesome code gold question :D – Martin Barker – 2018-08-16T15:52:41.280
2Closely related. – AdmBorkBork – 2018-08-16T15:57:52.913
2It may be worth adding a test case for a "unique" base -- e.g.
["zX", "tXdsyqzSDRP02", "brFNC02bc"] => "cb"
. (or whatever that should actually be, if that's incorrect) – Fund Monica's Lawsuit – 2018-08-16T23:01:34.1532I'd suggest a test case with more than 36 characters in the formats, to catch anyone using built-ins that only go up to base 36 – Jo King – 2018-08-17T00:21:29.700
1Are these positive numbers? Nonnegative? – Jakob – 2018-08-20T22:02:00.340
@Jakob none Signed numbers so positive because the work on string's if you will know if you see a
-
in front of it it's the negative and the programe does not need to do that. – Martin Barker – 2018-08-20T22:41:29.430So zero doesn't need to be supported? – Jakob – 2018-08-20T22:42:48.013
no Zero exists in none signed numbers, look at the test cases the first one shows how a 0 get's used in decimal to reverse decimal – Martin Barker – 2018-08-20T22:45:29.930
1@JoKing i have added a decimal to base 77 test case :D – Martin Barker – 2018-08-20T22:51:26.020
@MartinBarker I meant can the input number be zero (e.g.
0
in the base with digits0123456789
)? – Jakob – 2018-08-22T18:39:51.3231@Jakob oh no it does not have to work with 0. – Martin Barker – 2018-08-23T10:22:30.860